VMware Cloud Community
andypocock
Contributor
Contributor
Jump to solution

get-clusters | get-view resulting with The argument cannot be null or empty

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]

0 Kudos
1 Solution

Accepted Solutions
jeveenj
Enthusiast
Enthusiast
Jump to solution

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

-If you found this information useful, please consider awarding points for Correct or Helpful.

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

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

jeveenj
Enthusiast
Enthusiast
Jump to solution

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

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos
andypocock
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

andypocock
Contributor
Contributor
Jump to solution

appreciated, thank you very much.

0 Kudos
andypocock
Contributor
Contributor
Jump to solution

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 = @()

      1. Error trap with continue

#trap {

  1. write-host "Error but I will continue" -fore red

  2. 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

}

}

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
andypocock
Contributor
Contributor
Jump to solution

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

0 Kudos