VMware Cloud Community
mattssi
Contributor
Contributor
Jump to solution

Trouble with array/variable length

Yep, me again.

Everything seems to work well in this script I have. In this, vmsStillOn.length should return the number of machines powered on on a host.

This seems to work great until there is only 1 host on the machine. Here is what happens: If I have 2 VM's on a host, the length returns as 2. If I power down one of the VM's, the length doesn't display at all and I don't know why. I'm sure there is a better way to do a counter, but this is what I have so far to work with. Thanks IA!

do {

Start-Sleep -Seconds 15 #Wait 15 seconds, and check again!

$poweredOn = $FALSE

#Start-Sleep -Seconds 10

foreach ($machine in $hosts){

Connect-VIServer -Server $machine -User $user -Password $pass

{color:#ff0000} $vmsStillOn = Get-VMHost | Get-VM | where {$_.PowerState -ne "PoweredOff"}

Disconnect-VIServer -Confirm:$FALSE

Write-Host "Number of VMs is " $vmsStillOn.length

if ($vmsStillOn.length > 0){

$poweredOn = $TRUE

break

}

}

}while($poweredOn = $TRUE)

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

When there is only 1 guest running the variable $vmStillOn will not be an array of VirtualMachineImpl objects but a single VirtualMachineImpl object.

The length property does not exist in the VirtualMachineImpl object.

The length does exist for an array.

As a solution you could count the number of VirtualMachineImpl objects returned (with the Measure-Object cmdlet) and use that value instead of the length property.

Something like this for example

do {
	Start-Sleep -Seconds 15 #Wait 15 seconds, and check again!
	$poweredOn = $FALSE

	#Start-Sleep -Seconds 10
	foreach ($machine in $hosts){
		Connect-VIServer -Server $machine -User $user -Password $pass
		$vmsStillOn = Get-VMHost | Get-VM | where {$_.PowerState -ne "PoweredOff"} | Measure-Object | %{$_.Count}
		Disconnect-VIServer - Confirm:$FALSE

		Write-Host "Number of VMs is " $vmsStillOn
		if ($vmsStillOn > 0){
			$poweredOn = $TRUE
			break
		}
	}
}while($poweredOn = $TRUE) 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

When there is only 1 guest running the variable $vmStillOn will not be an array of VirtualMachineImpl objects but a single VirtualMachineImpl object.

The length property does not exist in the VirtualMachineImpl object.

The length does exist for an array.

As a solution you could count the number of VirtualMachineImpl objects returned (with the Measure-Object cmdlet) and use that value instead of the length property.

Something like this for example

do {
	Start-Sleep -Seconds 15 #Wait 15 seconds, and check again!
	$poweredOn = $FALSE

	#Start-Sleep -Seconds 10
	foreach ($machine in $hosts){
		Connect-VIServer -Server $machine -User $user -Password $pass
		$vmsStillOn = Get-VMHost | Get-VM | where {$_.PowerState -ne "PoweredOff"} | Measure-Object | %{$_.Count}
		Disconnect-VIServer - Confirm:$FALSE

		Write-Host "Number of VMs is " $vmsStillOn
		if ($vmsStillOn > 0){
			$poweredOn = $TRUE
			break
		}
	}
}while($poweredOn = $TRUE) 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mattssi
Contributor
Contributor
Jump to solution

That's beautiful. Thanks for the eplanation and work around!

Reply
0 Kudos