Error while checking EnabledState of a VM

Dec 27, 2011 at 8:35 AM

Hi,

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
Write-Host
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 {
 $i++
  
 ## Get the VMs Current State
 $VMState = Get-VMSummary $VMName
 $VMState
 $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       : 10.0.14.15
Jobs            : {Starting, Initializing, Starting}
VMElementName   : 10.0.14.15 - 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
   ception
    + FullyQualifiedErrorId : CannotIndex

Feb 10, 2012 at 1: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.