PowerCLI

Expand all | Collapse all

list the number of powered on vms per host in a cluster

  • 1.  list the number of powered on vms per host in a cluster

    Posted Dec 13, 2018 03:41 PM

    Hi Can somebody assist with a script to output the number of powered on vm's on each host allowing me to name a specific cluster on which to do this? I would like it to display the output in 2 columns; Host, NumVM,

    I've been trying to amend something I found online which gives a similar output but it goes through all clusters:

    Get-VMHost | Select @{N="Cluster";E={Get-Cluster -VMHost $_}}, Name, @{N="NumVM";E={($_ | Get-VM | where {$_.powerstate -eq "poweredon"}).Count}} | Sort Cluster, Name

    That gives 3 columns and goes through the clusters..... which I don't want to do. I'd like to explicitly list a cluster name and just list the hosts/vm numbers in the output.



  • 2.  RE: list the number of powered on vms per host in a cluster
    Best Answer

    Posted Dec 13, 2018 04:31 PM

    Try something like this

    $clusterName = 'MyCluster'

    Get-Cluster -Name $clusterName -PipelineVariable cluster |

    Get-VMHost |

    Select @{N="Cluster";E={$cluster.Name}}, Name,

        @{N="NumVM";E={(Get-View -ViewType VirtualMachine -SearchRoot $_.ExtensionData.MoRef -Property Runtime.PowerState -Filter @{'Runtime.PowerState'='poweredon'}).Count}} |

    Sort-Object -Property Cluster,Name



  • 3.  RE: list the number of powered on vms per host in a cluster

    Posted Dec 13, 2018 08:39 PM

    Thanks for this, works perfectly.