VMware Cloud Community
chrisbather
Contributor
Contributor
Jump to solution

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

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.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

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


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

chrisbather
Contributor
Contributor
Jump to solution

Thanks for this, works perfectly.

Reply
0 Kudos