VMware Cloud Community
AnotherEmployee
Contributor
Contributor

Powerstate heavily delayed

Hi everyone,

I made a script which restarts a couple of servers which have dependent services. So I'm checking if every server is properly shutdown with these lines:

while((Get-VM -Name $myServer).PowerState -ne "PoweredOff"){
    Start-Sleep -Seconds 5
}

This generally works, but it takes like 5 minutes more for the script to recognize that a machine is powered off, compared to what's shown in vSphere Client.

Is this a known problem or am I doing something wrong?

0 Kudos
5 Replies
LucD
Leadership
Leadership

How did you determine this 5 minute delay?

And no, this is not a known issue afaik.

If you want (somewhat) faster code, you could try the Get-View instead of the Get-VM

$sView = @{
  ViewType = 'VirtualMachine'
  Property = 'Runtime.PowerState'
  Filter = @{ Name = $myServer }
}
while ((Get-View @sview).Runtime.PowerState -ne 'poweredoff'){
  Start-Sleep -Seconds 5
}

 


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

AnotherEmployee
Contributor
Contributor

It's not exactly 5 minutes but something around this timespan.

Regarding the example you've shown, I don't need to run UpdateViewData() with every loop?

0 Kudos
LucD
Leadership
Leadership

No, it runs the Get-View each time.

Using UpdateViewData is another option.

$sView = @{
  ViewType = 'VirtualMachine'
  Property = 'Runtime.PowerState'
  Filter = @{ Name = $myServer }
}
$vmView = Get-View @sview
while ($vmView.Runtime.PowerState -ne 'poweredoff'){
  Start-Sleep -Seconds 5
  $vmView.UpdateViewData('Runtime.Power')
}


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

LucD
Leadership
Leadership

But I was not asking if it was exactly 5 minutes, I just wondered how you determined that major time difference


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

0 Kudos
AnotherEmployee
Contributor
Contributor

Oh, I see. I had the vSphere browser client opened up.

Edit: I'll try out the Get-View version on the next opportunity.

0 Kudos