Automation

 View Only
  • 1.  Report CPU, Core, and VMs

    Posted Jun 16, 2022 07:40 PM

    Hello everyone,

    I would like to get a report of the hosts contained in a cluster using PowerCLI containing the following information:

    Total CPU
    Total CPU Used
    Total CPU free
    Total Core
    Total Core Used
    Total Core free
    Total VMs that could be installed using configuration 1:4

    I have been able to get the total, used and free CPUs, but I can't get the cores information and VMs

    Could you please enlighten me and show me how to do it?

     



  • 2.  RE: Report CPU, Core, and VMs

    Posted Jun 16, 2022 07:51 PM

    What do you mean by "cores used"?
    Is that the number of vCPU assigned to VMs running in that cluster?
    Or do you mean realtime performance metrics showing core cpu utilisation?



  • 3.  RE: Report CPU, Core, and VMs

    Posted Jun 16, 2022 08:44 PM

    Sorry I explained wrong, what I intend to do is to make an estimate based on the cores that have the processors and the cores consumed , based on that estimate whose VMs are configured Core per Socket 1:4.

    I have created this script but I can't find the error, could you please help me?


    param(
    [Parameter(Mandatory = $false)]
    [String]$cluster,
    $vmhosts="*"
    )

    $vmhosts=get-cluster $cluster | Get-VMHost $vmhosts
    $vms=Get-VM

    $Output=@()

    ForEach ($vmhost in $vmhosts)
    {

    $vcpus=0
    $ratio=$null
    $hostthreads=$vmhost.extensiondata.hardware.cpuinfo.NumCpuCores
    $vms |Where-Object {$_.vmhost -like $vmhost}|ForEach {$vcpus+=$_.numcpu}
    if ($vcpus -ne "0") {$ratio= "$("{0:N2}" -f ($vcpus/$hostthreads))" + ":1"}

    $temp= New-Object psobject
    $temp| Add-Member -MemberType Noteproperty "Hostname" -value $vmhost.name
    $temp| Add-Member -MemberType Noteproperty "Cluster" -value $vmhost.parent.name
    $temp| Add-Member -MemberType Noteproperty "PhysicalThreads" -Value $Hostthreads
    $temp| Add-Member -MemberType Noteproperty "vCPUs" -Value $vcpus
    $temp| Add-Member -MemberType Noteproperty "Ratio" -Value $ratio
    $Output+=$temp

    }

    $output

     



  • 4.  RE: Report CPU, Core, and VMs
    Best Answer

    Posted Jun 17, 2022 08:54 AM

    I moved the Get-VM inside the loop, and it seems to work for me

    $vmhosts=get-cluster $cluster | Get-VMHost $vmhosts
    
    $Output=@()
    
    ForEach ($vmhost in $vmhosts)
    {
    $vms = Get-VM -Location $vmhost
    
    $vcpus=0
    $ratio=$null
    $hostthreads=$vmhost.extensiondata.hardware.cpuinfo.NumCpuCores
    $vms | ForEach {$vcpus+=$_.numcpu}
       if ($vcpus -ne "0") {$ratio= "$("{0:N2}" -f ($vcpus/$hostthreads))" + ":1"}
    
       $temp= New-Object psobject
       $temp| Add-Member -MemberType Noteproperty "Hostname" -value $vmhost.name
       $temp| Add-Member -MemberType Noteproperty "Cluster" -value $vmhost.parent.name
       $temp| Add-Member -MemberType Noteproperty "PhysicalThreads" -Value  $Hostthreads
       $temp| Add-Member -MemberType Noteproperty "vCPUs" -Value $vcpus
       $temp| Add-Member -MemberType Noteproperty "Ratio" -Value $ratio
       $Output+=$temp
    }
    
    $output


  • 5.  RE: Report CPU, Core, and VMs

    Posted Jun 17, 2022 10:25 AM

    Hi,

    I get the following error:

    Get-VMHost : Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is
    not null or empty, and then try the command again.
    At D:\vCollect-Reports\Untitled1.ps1:1 char:44
    + $vmhosts=get-cluster $cluster | Get-VMHost $vmhosts
    + ~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Get-VMHost], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVMHost

     

    I have updated to the latest version, but it gives the same error.



  • 6.  RE: Report CPU, Core, and VMs

    Posted Jun 17, 2022 10:27 AM

    I left out your parameter definitions, you will have to add them

    param(
    [Parameter(Mandatory = $false)]
    [String]$cluster,
    $vmhosts="*"
    )


  • 7.  RE: Report CPU, Core, and VMs

    Posted Jun 17, 2022 11:06 AM

    What parameters should I change so that the script runs on all vCenter clusters, and not one by one?



  • 8.  RE: Report CPU, Core, and VMs
    Best Answer

    Posted Jun 17, 2022 11:26 AM

    You could do something like this

    Get-Cluster -PipelineVariable cluster | ForEach-Object -Process {
      Get-VMHost -Location $cluster |
        ForEach-Object -Process {
          $numCpuSum = (Get-VM -Location $_ | Measure-Object -Property NumCpu -Sum).Sum
          New-Object -TypeName PSObject -Property @{
            Cluster         = $cluster.Name
            VMHost          = $_.Name
            PhysicalThreads = $_.ExtensionData.Hardware.CpuInfo.NumCpuCores
            vCPUs           = $numCpuSum
            Ratio           = if ($numCpuSum -gt 0) {
              "$("{0:N2}" -f ($numCpuSum/$_.ExtensionData.Hardware.CpuInfo.NumCpuCores))" + ":1"
            }
        }
      }
    }


  • 9.  RE: Report CPU, Core, and VMs

    Posted Jun 17, 2022 01:38 PM

    Thank you very much!!!!!