Error while checking EnabledState of a VM

Dec 27, 2011 at 7:35 AM


for a script I'm creating I want to shut down a machine. this is not a problem but while polling for "Stopped" I get error messages when it is in the running state coming from the vm1.ps1 script file. I guess I have the latest version since I downloaded the files yesterday, yet I didn't find the same error reported.

Following loop is executed

Write-Host "Shutting down VM"  -ForegroundColor Yellow
Invoke-VMShutdown -VM $VMName -Reason "Shutdown for export backup of VM" -Force

# --> There is no wait parameter for the shutdown available, so we must poll for it's state
$i = 1
$VMEnabledState = ""
Do {
 ## Get the VMs Current State
 $VMState = Get-VMSummary $VMName
 $VMEnabledState = $VMState.EnabledState
 Start-Sleep -s 5
while ($VMEnabledState -ne "Stopped" -or $i -gt 600)

Which generates the below output. As soon as the machine is in the stopped state, the errors are gone and the script continues (so it is not critical, but not nice either). Any idea what is causing this?

Shutting down VM

CPUCount        : 1
Snapshots       : 5
GuestOS         : Windows Server 2008 R2 Standard
Uptime          : 61870
UptimeFormatted : 12:01:01
CPULoadHistory  : {0, 0, 0, 0...}
Host            : SRVBLADE3
IpAddress       :
Jobs            : {Starting, Initializing, Starting}
VMElementName   : - SRVQAVXML
Notes           :
Heartbeat       : OK
FQDN            : SRVQAVXML
CPULoad         : 0
CreationTime    : 20110823145108.509710-000
EnabledState    : Running
Name            : B27E5860-596F-4DE7-B982-DA8430876DED
MemoryUsage     : 2048

Unable to index into an object of type System.Management.Automation.PSObject.
At C:\program files\modules\hyperv\VM.ps1:505 char:47
+             if ($VM.EnabledState -ne $vmstate[ <<<< "Running"]) {
    + CategoryInfo          : InvalidOperation: (Running:String) [], RuntimeEx
    + FullyQualifiedErrorId : CannotIndex

Feb 10, 2012 at 12:26 PM

It is becasue of line 221 in VM.ps1. I remarked it and no longer recieve the error. Be sure to move the curly bracket at the end to a new line.