JasonB71
Contributor
Contributor

PowerCLI to monitor reboots - VMTools not refreshing in order to check status

Jump to solution

What is the correct way to monitor a VM reboot?  I have a script to patch Windows VM Templates via PowerCLI Invoke-VMScript command.  I use the following script code to check the tools status.

do { (Write-Host "Waiting on server $vm to finish shutting down." -ForegroundColor Red),(Start-Sleep 1) }

   while ( (Get-VM $vm).extensiondata.Guest.ToolsStatus -ne "toolsNotRunning" -and $startDate.AddMinutes(5) -gt (Get-Date))

   Write-Host "VMTools down. Moving on." -ForegroundColor Green  

   do { (Write-Host "Waiting on server $vm to finish booting." -ForegroundColor Red),(Start-Sleep 5) }

   while ( (Get-VM $vm).extensiondata.Guest.ToolsStatus -eq "toolsNotRunning" )

   Write-Host "VMTools responding. Moving on." -ForegroundColor Green

Problem is when the VM reboots the tools never shows its not running before it starts back up.  If I refresh the webclient it works.  Is there a way to add code to refresh the VMTools status every second to see that the tools stop before checking its up again?

The reason is when patches are installing it take mins or up to hours.  By seeing the tools stopped the script knows to start looking for the tools to start before continuing on or it will start the next step prematurely.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

If I understand your question correctly, you are trying to implement the following

- wait till the VMware Tools stopped running

- then wait till the VMware Tools are running again

The basic issue is that a .NET object is not automatically refreshed.
You could do the Get-VM once more inside each loop, that way you have the latest info.

A vSphere object is neither updated automatically, but you can call the UpdateViewData method to refresh the content behind ExtensionData.

Something like this

$vm = Get-VM -Name $vm.Name

while($vm.ExtensionData.Guest.ToolsStatus -ne 'toolsNotRunning'){

    Start-Sleep 1

    $vm.ExtensionData.UpdateViewData()

}


while ( $vm.extensiondata.Guest.ToolsStatus -ne 'toolsRunning'){

   Start-Sleep 5

   $vm.ExtensionData.UpdateViewData()

}

Personally I do think that this method of watching a Guest OS reboot has some potential flaws.

If things happen quickly, or there is a hiccup with returning the updated properties, the script might get stuck in a loop.

A better solution imho, is to avoid doing this with a Guest OS reboot.

I'm more in favour of doing a Guest OS shutdown and then powering on the VM.

This allows one to monitor for Guest OS external events like a VMPoweredOffEvent and once there, wait for the GuestOperationsReady property to become $true.


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

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership

If I understand your question correctly, you are trying to implement the following

- wait till the VMware Tools stopped running

- then wait till the VMware Tools are running again

The basic issue is that a .NET object is not automatically refreshed.
You could do the Get-VM once more inside each loop, that way you have the latest info.

A vSphere object is neither updated automatically, but you can call the UpdateViewData method to refresh the content behind ExtensionData.

Something like this

$vm = Get-VM -Name $vm.Name

while($vm.ExtensionData.Guest.ToolsStatus -ne 'toolsNotRunning'){

    Start-Sleep 1

    $vm.ExtensionData.UpdateViewData()

}


while ( $vm.extensiondata.Guest.ToolsStatus -ne 'toolsRunning'){

   Start-Sleep 5

   $vm.ExtensionData.UpdateViewData()

}

Personally I do think that this method of watching a Guest OS reboot has some potential flaws.

If things happen quickly, or there is a hiccup with returning the updated properties, the script might get stuck in a loop.

A better solution imho, is to avoid doing this with a Guest OS reboot.

I'm more in favour of doing a Guest OS shutdown and then powering on the VM.

This allows one to monitor for Guest OS external events like a VMPoweredOffEvent and once there, wait for the GuestOperationsReady property to become $true.


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

0 Kudos