VMware Cloud Community
G0nz0UK
Enthusiast
Enthusiast

Command to show number of on and off VMs

Hello,

I've been running this to get the number VM on each host but it seems to lie:

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx -V2
    $platform = $esxcli.hardware.platform.get.Invoke()
    $cpu = $esxcli.hardware.cpu.global.get.Invoke()
    [PSCustomObject]@{
            Name = $esx.Name
            NoVM = $esx.ExtensionData.VM.Count
                }
                }

 

I checked 2 hosts and the script says 1 host has 0 and the other 4.  Will in vCenter both have 0 VMs.

Any ideas? I'd actually like 2 columns really showing powered on and off VMs.

Weird stuff going on.

Thanks

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

That includes Templates, to exclude those you could do

$esx.ExtensionData.VM.where{-not (Get-View -Id $_).Config.Template}.Count


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership

To get you 2 columns, you could do something like this

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
  $groups = (Get-View -Id $esx.ExtensionData.VM).where{ -not $_.Config.Template} |
    Group-Object -Property {$_.Runtime.PowerState}
    New-Object -TypeName PSObject -Property ([ordered]@{
      VMHost = $esx.Name
      VMPoweredOn = $groups.Where{$_.Name -eq 'poweredOn'}.Group.Count
      VMPoweredOff = $groups.Where{ $_.Name -eq 'poweredOff'}.Group.Count
    })
  } 


But this might be simpler

 

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    New-Object -TypeName PSObject -Property ([ordered]@{
      VMHost = $esx.Name
      VMPoweredOn = (Get-VM -Location $esx).where{$_.PowerState -eq 'PoweredOn'}.Count
      VMPoweredOff = (Get-VM -Location $esx).where{ $_.PowerState -eq 'PoweredOff' }.Count
    })
  }


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
G0nz0UK
Enthusiast
Enthusiast

Oh yes that worked so well, thank.

Last question, if that is ok.  I can get the number of CPUs and Sockets via the below script, do you know the command for logical processors?   I guess I could do socket x cpus?

Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
    $esxcli = Get-EsxCli -VMHost $esx -V2
    $platform = $esxcli.hardware.platform.get.Invoke()
    $cpu = $esxcli.hardware.cpu.global.get.Invoke()
    [PSCustomObject]@{
        Cluster = (Get-Cluster -VMHost $esx).Name
        Name = $esx.Name
        VMPoweredOn = (Get-VM -Location $esx).where{$_.PowerState -eq 'PoweredOn'}.Count
        VMPoweredOff = (Get-VM -Location $esx).where{ $_.PowerState -eq 'PoweredOff' }.Count
        Processor = $esx.ExtensionData.Hardware.CpuPkg[0].Description
        NumCPU = $esx.NumCpu
        ProcSpeedGhz = [math]::Round($esx.ExtensionData.Hardware.CpuInfo.Hz/(1000*1000*1000),1)
        ProcSockets = $cpu.CPUPackages
        ProcCoresPerSocket = $cpu.CPUCores / $cpu.CPUPackages
        NoNics = $esxcli.network.nic.list.Invoke().Count
    }

 

 

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

In $esx.EXtensionData.Config.HyperThread you can find out if hyperthreading is active.
With that info and CPUCores you should be able to find the Locgical CPU


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos