Pages

7/10/2013

Powershell: Count datastores on VMWare Hosts

Powershell: Count datastores on VMware Hosts

All the LUN mappings & datastore names need to match on all hosts in a cluster. I hope to someday script a more comprehensive comparison of datastores & names. However, a quick and dirty confirmation is to count the datastores that are connected on every host in each cluster. If they match, it at least gives me a warm feeling.

#####################################################################
#
#   
#        Gather count of LUNs/Datastores connected to all hosts
#

$vcs = Read-Host "vCenter"
$user = Read-Host "userid"
$pw = Read-Host "Password for $user" -AsSecureString
#convert $pw to plain text
    $pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
        [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw))

connect-viserver -Server $vcs -User $user -Password $pass

$pass = " "

$today = get-date
$day = $today.Day
$mth = $today.Month
$year = $today.Year
$hour = $today.Hour
$min = $today.Minute
$sec = $today.Second
$date = "$year-$mth-$day-$hour$min$sec"
$outfile = ".\datastores-"+$vcs+"-"+$date+".csv"

clear-host

"Host-Name,Datastore-Count" | out-file $outfile -encoding ascii
foreach ($VMHost in (Get-VMHost -Location $Cluster)) {
"    $VMHost"
    $dstores = $VMHost | Get-Datastore
    $ds = $dstores.count
"        $ds"
    "$VMHost,$ds" | out-file $outfile -encoding ascii -append
}

disconnect-viserver -confirm:$false

No comments: