Get Datastore Visibility between hosts at the same Cluster on many vCenters

Version 2

    Here is a script based on the Validate datastore visibility between hosts | ForwardOrReverse.

     

    By altering the $VcStringList , you can add yout vcenters and generate a list with the info you need. =)

     

    Again: Credits for Eric

     

    <#
    ==================================================================
    Author(s):           Guilherme Alves Stela <guistela@br.ibm.com>
    Based on the script "Validate-Datastores" from http://wannemacher.us/?p=445
    Credits for: Eric
    File:                Get-DatastoreVisibility.ps1
    Date:                19-03-2014
    ==================================================================
    Disclaimer: This script is written as best effort and provides no
    warranty expressed or implied. Please contact the author(s) if you
    have questions about this script before running or modifying.
    ==================================================================
    #>
    ############################################## Adding PowerCLI Snapin
    if(!(get-pssnapin | where {$_.name -eq "vmware.vimautomation.core"})) {
            try { 
               add-pssnapin VMware.VimAutomation.Core
            } catch {
                $ErrorMessage = $_.Exception.Message
                Write-Host "$ErrorMessage"
            }
    }
    ######################################################
    ######################################################Set to multiple VC Mode 
    try{
    if(((Get-PowerCLIConfiguration -Scope Session).DefaultVIServerMode) -ne "Multiple") {
       Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope AllUsers -Confirm:$false
       Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false
       Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope Session -Confirm:$false
    }}catch{
          $ErrorMessage = $_.Exception.Message
          Write-Host "$ErrorMessage" 
    }
    ######################################################
    function Validate-Datastores{
    <#
    .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 (Get-Cluster cluster1 | Get-VMHost)
    #>
    param (
        [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl[]]$vmhosts = $(throw "vmhosts must be specified"),$vcenter,$cluster
    )
    $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 vCenter,Cluster,VMHost, Datastore, Status
            $info.vCenter = $vcenter
            $info.Cluster = $cluster
            $info.VMHost = $vmhost.Name
            $info.Datastore = $dsName
            $info.Status = $testList[$dsName]
            $info
        }
    }
    }
    ############################################## start connectvCenters
    function connectvCenters{
        param($list)
     $arrayConnectedVC =@()
          foreach($itemList in $list){ 
                try{
                    $sessionID = $global:defaultviservers | where {$_.Name -eq $itemList} |Select -First 1 -ExpandProperty SessionId
                    if($sessionId){
               $vc = Connect-VIServer -server $itemList -Session $sessionId -wa 0 
                        Write-Host "Reconectado ao servidor '$vc'"
                        $arrayConnectedVC += $vc     
              }else{
                        $vc = Connect-VIServer -server $itemList -wa 0
                        Write-Host "Conectado ao servidor '$vc'"
                        $arrayConnectedVC += $vc
                    }
                }catch{
                $ErrorMessage = $_.Exception.Message
                    Write-Host "Não foi possível conectar ao servidor '$vc' `n  $ErrorMessage"
                } 
           }
        return $arrayConnectedVC
    }
    ############################################## end connectvCenters     
    #get a list of vcenters from file
    try{
        #alter here the path of the file you want to use
        $VCStringlist = get-content C:\temp\lista_vcenters.csv -ea stop
    }catch{
        $ErrorMessage = $_.Exception.Message
        Write-Host "Erro ao obter lista de vCenters do arquivo! `n $ErrorMessage"
    }
    #connect all vcenters in the file
    $conns = connectvCenters $VCStringlist
    Clear
    $outputInfo =@()
    #loop between vcenters 
    foreach ($vc in $conns){
    Write-Host "`tGetting Info from $vc"
        #loop between clusters
        Get-Cluster -Server $vc |Foreach{
          Write-Host "`t`tGetting Info from $_"
          $outputInfo += Validate-Datastores ($_ | Get-vmhost) $vc.Name $_.name
        }
    }
    $dataTD = get-date
    $dd = $dataTD.Day
    $mm =$dataTD.Month
    $yy = $dataTD.Year
    $hh = $dataTD.Hour
    $mmm = $dataTD.Minute
    $ss = $dataTD.Second
    $TimeStamp = "$dd-$mm-$yy $hh"+"h"+"$mmm"+"m$ss"+"s" 
    $path = "c:\temp\DatastoreVisibility$TimeStamp.csv"
    $outputInfo | export-csv -Path $path -UseCulture -NoTypeInformation
    write-host "Arquivo exportado: $path"