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
{
param([string]$host1,[string]$host2)
$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
{
param([string]$host1,[string]$host2)
$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
{
param([string]$datastore)
Get-VMHost -Datastore (Get-Datastore $datastore) | Sort Name | %{$_.Name}
}

PS D:Scripts> Get-DatastoreHosts DATASTORE_TEST1

esxServer2
esxServer3

Thank Microsoft for Powershell and the Compare-Object cmdlet!

Hugo

»crosslinked«

12 thoughts on “Compare ESX configurations with Powershell”

  1. Pingback: Compare specific ESX configuration items « H9Newser’s Blog
  2. Hi Hugo,

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

    param([string]$host1,[string]$host2)
    $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}
    $a

    i get all my datastores?

    cheers

    1. 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.
      Hugo

  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!

    Paul

  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”.

    Manually:
    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.

    Profile:
    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: http://www.peetersonline.nl/index.php/powershell/build-your-own-powershell-universe/

    Hugo

Leave a Reply