Count VM’s on ESX host

Categories: PowerShell

  • This one-liner will count all VM’s on a ESX host
  • Get-VMHost -Server xxx.xxx.xxx.xxx
    | Select Name,@{N="NumVM";E={($_ |Get-VM -Server xxx.xxx.xxx.xxx
    | where {$_.PowerState -eq "PoweredOn"}).Count}}
    
  • Coding for usage with the VESI:
  • $noConnection = $true
    
    if ($global:vmwareManagedHosts) {
        foreach ($key in @($global:vmwareManagedHosts.Keys)) {
            if (($managedHost = $global:vmwareManagedHosts[$key]) -and ($managedHost.Connected)) {
                $noConnection = $false
    			Get-VMHost -Server $managedHost.connection | Select Name,@{N="NumVM";E={($_ |Get-VM -Server $managedHost.connection | where {$_.PowerState -eq "PoweredOn"}).Count}} | Sort Name
    			}
    	}
    }
    if ($noConnection) {
        [System.Windows.Forms.MessageBox]::Show('You must connect to one or more hosts before you can manage the hosts themselves. Please click on the ''Managed Hosts'' node, connect to one or more of the servers you have configured there, and then try again.','Connection not established',[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null
    }
    
    «
    »