Validate datastore visibility between hosts

This script will check a set of hosts and report on whether the available datastores match. The first host found becomes the reference host and the others are compared against it.

param (
    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]$vmhosts = $(throw "vmhosts must be specified")
)
 
$masterList = @{}
 
# Use the first host as the reference host
foreach ($datastore in Get-Datastore -VMHost $vmhosts[0]) {
    $masterList[$datastore.Name] = "Missing"
}
 
# Check all of the hosts against the master list
foreach ($vmhost in $vmhosts) {
    $testList = @{} + $masterList
 
    foreach ($datastore in Get-Datastore -VMHost $vmhost) {
        $dsName = $datastore.Name
 
        # If we have a match change the status
        if ($testList.ContainsKey($dsName)) {
            $testList[$dsName] = "OK"
        }
        # Otherwise we have found a datastore that wasn't on our reference host.
        else {
            $testList[$dsName] = "Extra"
        }
    }
 
    # Output our findings
    foreach ($dsName in $testList.Keys) {
        $info = "" | Select-Object VMHost, Datastore, Status
        $info.VMHost = $vmhost.Name
        $info.Datastore = $dsName
        $info.Status = $testList[$dsName]
        $info
    }
}
<#
.SYNOPSIS
Check a set of hosts to see if they see the same datastores.  This script
does not filter local datastores.
 
.PARAMETER clusters
An array of the hosts you want to check.
 
.EXAMPLE
.\Validate-Datatores.ps1 (Get-Cluster cluster1 | Get-VMHost)
#>

Comments (5)

  1. kk

    Hi Eric,

    how to export this to CSV file?
    thanks.

  2. Eric (Post author)

    @kk

    Since the script is outputting PowerShell objects you can just pipe it to the Export-Csv cmdlet.

    .\Validate-Datatores.ps1 (Get-Cluster cluster1 | Get-VMHost) | Export-Csv exportFile.csv

  3. vineeth

    Hi Eric

    Thanks for the script this was very use ful to me,But here i need one more info (In the same data i need nna of the luns)can you help me out.

  4. Thom

    Very useful script! Didn’t realise how little input was required.

    Many thanks Eric!

  5. Guilherme Stela

    Very Nice!!! I’ve created another script based on yours to verify an entire environment. Published here=>https://communities.vmware.com/docs/DOC-26036
    Thanks for sharing! =)

Leave a Comment

Your email address will not be published.