VMware Cloud Community
slayer991
Enthusiast
Enthusiast
Jump to solution

Powerstate status - Issue with code block - returns false but appears true when listing extended properties

So this section of code (part of a much larger script) does not appear to work correctly.  It returns false when it should return true.  Basically, I want my script to check to see that VMs are powered on by the admin running the script.  It will pause on failure and then press any key to retry.  Here's the code block (which I split out into it's own script for testing).

#Import the CSV

$vmlist = Import-CSV d:\scripts\migration.csv

#validate VMs are powered up on the new vCenter

foreach ($item in $vmlist) {

  $vmname=$item.vmname

  $powerstate = (Get-VM $vmname).extensiondata.Guest.PowerState -eq "poweredOn"

  IF ($powerstate -eq $false) {

  write-host ""

  write-host "$vmname has failed to start, please confirm the VM is up.  Script will pause." -foreground Yellow

  write-host "Bring $vmname backonline then" -foreground Yellow

  Write-Host "Press any key to retry..." -foreground Yellow

  Write-Host ""

  $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')

  }

     ELSE {

  write-host ""

  write-host "$vmname is powered up, script will continue" -foreground green

  write-host ""

  }

}

When listing the extended properties it returns the correct info:

PowerCLI D:\scripts> Get-VM ATEST-VM002| format-list *

PowerState              : PoweredOn

When I run it in the script with the extensiondata.Guest.Powerstat it returns false.

PowerCLI D:\scripts> (Get-VM ATEST-VM002).extensiondata.Guest.PowerState -eq "PoweredOn"

False

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Shouldn't that be

(Get-VM $vmname).extensiondata.Runtime.PowerState -eq "poweredOn"

But why look in the ExtensionData, that same property is exposed as (Get-VM $vmname).PowerState


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Shouldn't that be

(Get-VM $vmname).extensiondata.Runtime.PowerState -eq "poweredOn"

But why look in the ExtensionData, that same property is exposed as (Get-VM $vmname).PowerState


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

0 Kudos
slayer991
Enthusiast
Enthusiast
Jump to solution

(Get-VM $vmname).PowerState did the trick.  I don't know why I chose the other method (I think I pulled it from an old script of mine). Thanks LucD!

0 Kudos