Recently one of my trusty scripts has stopped working - any words of wisdom and advice will be gratefully received.
SCRIPT:
$clusters = Get-Cluster | get-view
foreach ($cluster in $clusters)
{
$1stESXinCluster = Get-VIObjectByVIView -MORef $cluster.Host[0]
}
RESULT:
The argument cannot be null or empty.
At :line:5 char:46
+ $1stESXinCluster = Get-VIObjectByVIView -MORef <<<< $cluster.Host[0]
It might be possible, one of the cluster which is in $cluster is without a host.
You can add conditional operator in this.
$clusters = Get-Cluster | get-view foreach ($cluster in $clusters) { if ($cluster.Host[0]-ne $null){ $1stESXinCluster = Get-VIObjectByVIView -MORef $cluster.Host[0] } }
Hope this helps
Could it be that you have a cluster without any ESX servers in it ?
Btw why don't you use
Get-Cluster | %{$_ | Get-VMHost | select -First 1}
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It might be possible, one of the cluster which is in $cluster is without a host.
You can add conditional operator in this.
$clusters = Get-Cluster | get-view foreach ($cluster in $clusters) { if ($cluster.Host[0]-ne $null){ $1stESXinCluster = Get-VIObjectByVIView -MORef $cluster.Host[0] } }
Hope this helps
Ah and eureka, yes there is a cluster with no hosts in; thank you very much makes perfect sense now you've explained.
Your suggested command string works perfectly too, I am using this to get the first ESX host to query datastores (used/free and contents) per cluster getting results into one report.
So could I use something along the lines of
$1stESXhosts = Get-Cluster | %{$_ | Get-VMHost | select -First 1}
foreach ($1sthost in $1stESXhosts)
{ get-datastore ......}
Thanks
Andy
Yes, that should be possible
$1stESXhosts = Get-Cluster | %{$_ | Get-VMHost | select -First 1} foreach ($1sthost in $1stESXhosts) { $1sthost | get-datastore }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
appreciated, thank you very much.
Luc - just hoping you can offer a bit more advice..?? below is the script which previously (using the Get-VIObjectByVIView -MORef $cluster.host[0] to determine 1st host) would list each cluster and all associated datastores.
I am tying to incoprporate your command structure (which did get round the null result) and have slightly changed to fit purpose, but now i get the report for all cluster but only for datastores associated to the frist and only esx host
$DatastoreReport = @()
Error trap with continue
#trap {
write-host "Error but I will continue" -fore red
continue
#}
$clusters = Get-Cluster | Get-View
foreach($cluster in $clusters)
{
$1stESXinCluster = Get-VMHost | select -First 1
#$1stESXinCluster = Get-VIObjectByVIView -MORef $cluster.host[0]
#$VMcount = (Get-VIObjectByVIView -MORef $cluster.MoRef | Get-VM).Count
$datastores = $1stESXinCluster | Get-Datastore
foreach($ds in $datastores)
{
$row = "" | Select ClusterName, DatastoreName, VMcount, DScapacity, DSused, DSfree
$row.ClusterName = $cluster.Name
$row.DatastoreName = $ds.Name
$row.VMCount = $VMcount
$row.DScapacity = $ds.CapacityMB
$row.DSused = $ds.CapacityMB - $ds.FreeSpaceMB
$row.DSfree = $ds.FreeSpaceMB
$DatastoreReport += $row
}
}
You should get the ESX hosts for a specific cluster.
Like this
$DatastoreReport = @() $clusters = Get-Cluster foreach($cluster in $clusters) { $1stESXinCluster = $cluster | Get-VMHost | select -First 1 $datastores = $1stESXinCluster | Get-Datastore foreach($ds in $datastores) { $row = "" | Select ClusterName, DatastoreName, VMcount, DScapacity, DSused, DSfree $row.ClusterName = $cluster.Name $row.DatastoreName = $ds.Name $row.VMCount = $VMcount $row.DScapacity = $ds.CapacityMB $row.DSused = $ds.CapacityMB - $ds.FreeSpaceMB $row.DSfree = $ds.FreeSpaceMB $DatastoreReport += $row } }
Btw there is no need to do the Get-View after the Get-Cluster cmdlet.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thought I had tried using the $cluster variable... this worked a treat so many thanks, there was an null argument error again (for the zero host cluster) but it continued.
Thanks for your help
Andy