stony007_de
Enthusiast
Enthusiast

how to get vm-hosts in cluster

hi dev's,

i have 2 clusters with ESX Hosts.

Now I need to get the number of host in each cluster.

My Quick&Dirty workaround is very slow!

First i get all Clusters in Environment, then the reading the Host Information's and last the Count of the Hosts... In result ~120Sec!! !http://communities.vmware.com/images/emoticons/sad.gif!

is there an other option to get this information?

--stony007_de

Tags (2)
0 Kudos
4 Replies
nnedev
VMware Employee
VMware Employee

Hi,

You can use this piece of code to get the number of hosts in a cluster:

(Get-VMHost -Location myCluster).Count

Is this faster than your solution?

Regards,

Nedko Nedev

PowerCLI Development Team

Regards, Nedko Nedev PowerCLI Development Team
avlieshout
VMware Employee
VMware Employee

Nedko,

That code doesn't work if the cluster is empty or contains just one host.

You need to use array subexpression for this

@(Get-VMHost -Location myCluster).Count

Cheers,

Arnim

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
nnedev
VMware Employee
VMware Employee

Good point! :smileyblush:

Regards,

Nedko Nedev

PowerCLI Development Team

Regards, Nedko Nedev PowerCLI Development Team
0 Kudos
RvdNieuwendijk
Leadership
Leadership

The next script gives the number of hosts per cluster in a different way. It works fast but it needs PowerCLI v4.1.

Get-Cluster | ForEach-Object {
  $Report = "" | Select-Object -Property Cluster,HostsCount
  $Report.Cluster = $_.Name
  $Report.HostsCount = $_.ExtensionData.Host.Count
  $Report
} 

You can also do this in a oneliner:

Get-Cluster | Select-Object Name,@{N="HostsCount";E={$_.ExtensionData.Host.Count}}

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos