vmware - Unable to return info on hosts in multiple clusters in PowerShell using PowerCLI -
i have powershell script used return information each host on user specified cluster. user provides vcenter , cluster in parameters , script works expected.
i trying modify script user needs pass in vcenter parameter , return information hosts on clusters.
here original script have works:
param( $vc, $clustername ) add-pssnapin vmware.vimautomation.core connect-viserver $vc $vmhosts = get-cluster $clustername | get-vmhost | ? { $_.connectionstate -eq "connected" } | sort-object -property name foreach ($vmhost in $vmhosts) { get-vmhoststorage -rescanallhba -vmhost $vmhost | out-null $esx = get-vmhost $vmhost foreach($hba in (get-vmhosthba -vmhost $esx -type "fibrechannel")){ $target = ((get-view $hba.vmhost).config.storagedevice.scsitopology.adapter | {$_.adapter -eq $hba.key}).target $luns = get-scsilun -hba $hba -luntype "disk" $nrpaths = ($target | %{$_.lun.count} | measure-object -sum).sum $deadpaths = $luns | get-scsilunpath | group-object -property state | ? { $_.name -eq "dead"} $hbadevice = $hba.device $targetcount = $target.count $lunscount = $luns.count $deadpathcount = $deadpaths.count "vmhost=$vmhost;hba=$hbadevice;targets=$targetcount;devices=$lunscount;paths=$nrpaths;deadpaths=$deadpathscount|" } } disconnect-viserver -confirm:$false
and here modified version:
param( $vc ) add-pssnapin vmware.vimautomation.core connect-viserver $vc $clusters = get-cluster foreach ($cluster in $clusters) { $clustername = $cluster.name $vmhosts = get-cluster $clustername | get-vmhost | ? { $_.connectionstate -eq "connected" } | sort-object -property name foreach ($vmhost in $vmhosts) { get-vmhoststorage -rescanallhba -vmhost $vmhost | out-null $esx = get-vmhost $vmhost foreach($hba in (get-vmhosthba -vmhost $esx -type "fibrechannel")){ $target = ((get-view $hba.vmhost).config.storagedevice.scsitopology.adapter | {$_.adapter -eq $hba.key}).target $luns = get-scsilun -hba $hba -luntype "disk" $nrpaths = ($target | %{$_.lun.count} | measure-object -sum).sum $deadpaths = $luns | get-scsilunpath | group-object -property state | ? { $_.name -eq "dead"} $hbadevice = $hba.device $targetcount = $target.count $lunscount = $luns.count $deadpathcount = $deadpaths.count "vmhost=$vmhost;hba=$hbadevice;targets=$targetcount;devices=$lunscount;paths=$nrpaths;deadpaths=$deadpathscount|" } } } disconnect-viserver -confirm:$false
the error getting is:
could not execute powershell command. @ \\xx\xxx\xxxx\scripts\vmwarepathcheckallclusters.ps1:35 char:26 + get-vmhoststorage <<<< -rescanallhba -vmhost $vmhost | out-null
it seems $vmhost
being return null, can't work out why!
this first time using powercli commandlets , new powershell well. i'm sure simple , appreciate help. if require anymore information i'll more happy provide. bump!
edit: here additional error info:
+ get-vmhoststorage <<<< -rescanallhba -vmhost $vmhost | out-null ---> vmware.vimautomation.sdk.types.v1.errorhandling.vimexception.vimexception: 9/24/2014 5:58:57 get-vmhoststorage value cannot found mandatory parameter vmhost ---> system.management.automation.parameterbindingexception: value cannot found mandatory parameter vmhost
have tested running get-vmhoststorage
without -rescanallhba
? why think $vmhost
being returned null? maybe finds host, nothing rescan? guess.
not sure cause problem, don't need run get-cluster
twice. rather,
$vmhosts = $cluster | get-vmhost | ? <etc>
edit - given run of script, how many times full error text appear? how many vmhosts found in run? have cluster without hosts? host without storage? set $vmhost = $null
or $cluster = $null
if value 1 loop iteration seems corrupting next iteration. (sorry, there's gotta better way phrase that... catch drift.) put offending line of code in [try]
block , handle error catch.
edit again - apparently this fixed host in maintenance mode: if (!$vmhost) {continue}
Comments
Post a Comment