VMware Cloud Community
brekus
Enthusiast
Enthusiast

How can I compare object's within an array - for host versions

Hey all. I am trying write something to compare the host versions of the hosts within a cluster.  I want to make sure they are all the same and report if one is different.  I have looked at compare-objects and that needs two objects to reference and I don't think that will work.  Does anyone know of a way I can just find the "odd man out" if I can get the host versions into an array or variable?

Thanks

0 Kudos
2 Replies
LucD
Leadership
Leadership

One way you could do this would be with the Group-Object cmdlet

$array | Group-Object -Property Version

If this returns more then 1 group, there are differing versions present.

It assumes the $array variable holds objects that have a Version property


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

0 Kudos
mattandes
Enthusiast
Enthusiast

You could try something along the lines of this.

$vmhosts = Get-VMhost | Group Version

If (($vmhosts | measure).count -gt 1) {

    $output = $vmhosts | Select @{N="Version";E={$_.Name}}, Count, @{N="Hosts";E={$_.Group}}

    $output

}

If there are more than one version for the hosts it will out put the version, the number of hosts with that version, and the hosts with that version in the $output variable. If it doesn't give any output it won't display anything. If you want to always get output you could change the 1 in the second line to 0 and you'll always get output. As per your original post you can limit by cluster by changing the first line to something like:

$vmhosts = Get-Cluster cluster | Get-VMhost | Group Version

Blog: http://www.virtual-matt.net
0 Kudos