Compare ESX configurations with Powershell

One of the challenges in managing a large VMware Infrastructure is keeping all ESX Servers within a cluster equal. This is essential for having vmotion capabilities and therefore essential for a solid HA configuration. I have showed you earlier how to add the LUN Count for each ESX Server to your VI Client. This allows you to spot differences quickly. But finding exactly which datastores are missing on which ESX Server can be a bigger challenge.

Here are some small functions that can help you determine where the major differences are.

Comparing datastores:

function Compare-VMHostDatastores
$a = Get-VMHost $host1 | Get-Datastore | %{$_.Name}
$b = Get-VMHost $host2 | Get-Datastore | %{$_.Name}
Compare-Object $a $b

Compare-VMHostDatastores esxServer1 esxServer2

InputObject                         SideIndicator
———–                                ————-
esxServer1_Local             <=
esxServer2_Local             =>
DATASTORE_TEST1         =>

And comparing Port Groups:

function Compare-VMHostPortgroups
$a = Get-VirtualPortGroup (Get-VMHost $host1) | %{$_.Name}
$b = Get-VirtualPortGroup (Get-VMHost $host2) | %{$_.Name}
Compare-Object $a $b

Compare-VMHostPortgroups esxServer1 esxServer2

InputObject                SideIndicator
———–                      ————-
PortGroup_TEST     <=
Internal                       <=

Maybe you prefer to go the other way around and check to which ESX servers a specific datastore is attached?

function Get-DatastoreHosts
Get-VMHost -Datastore (Get-Datastore $datastore) | Sort Name | %{$_.Name}

PS D:Scripts> Get-DatastoreHosts DATASTORE_TEST1


Thank Microsoft for Powershell and the Compare-Object cmdlet!


12 thoughts on “Compare ESX configurations with Powershell

  1. […] posted a great script. It will compare configuration items between a given set of hosts. This can especially come in […]

  2. Hi Hugo,

    any idea why when i run this script i only get back my local datastores?

    $a = Get-VMHost $host1 | Get-Datastore | %{$_.Name}
    $b = Get-VMHost $host2 | Get-Datastore | %{$_.Name}
    Compare-Object $a $b

    but if i run
    $a = Get-VMHost $host1 | Get-Datastore | %{$_.Name}

    i get all my datastores?


    • Hi Paul,
      Be glad. The local datastores are the only difference between the two hosts. Add the -IncludeEqual switch to the Compare-Object line to show all datastores.

  3. Hi Hugo,

    I was expecting to get back all my datastores with the ones being equal having a == sign in the SideIndicator. But having read the help on compare-objectshow them.

    thanks for that!


  4. Hugo,

    I assume you have Powershell and the VI Toolkit installed. Start the VI Toolkit console (which is essentially Powershell with the VI Toolkit snapin loaded). Connect to Virtual Center using “Connect-VIServer MYVCSERVER”.

    Copy and paste the functions into the window and press enter a few times. Then you can use the functions by typing the name and supplying two host names or a datastore name (see the second quotes for examples). When you close the window, the functions are forgotten.

    Add the functions to your profile in order to have them available every time you open the Powershell / VI Toolkit window. Read this for more info:


Leave a Reply