Wait for vm to fully start up until windows is applying updates

This post was originally published on this site

With the below script I am trying to perform shutdown vm and apply windows updates and power on it and wait till windows updates are fully applied and then poweroff the machine.

Steps as below:

1.Do guest shutdown for the vm which is $vm variable and wait till powerstate becomes poweredOff.

2. Start back the vm and wait till Guest.GuestOperationsReady becomes True

3. Then finally poweroff the vm using Stop-VMGuest.

 

Here in step 2  when I start the vm it is applying the windows updates. However the Guest.GuestOperationsReady state shows True so its not waiting for the guest to completely startup in a whileloop. It just moved to step 3 and the windows updates are not applying as expected. Is there any way to wait till we see ctrl+alt+del screen in windows and move to step 3.

 

foreach($vm in (Get-VM Test01,Test02)){
Write-Host "Stopping VM $($vm.Name) to Apply windows updates"
Stop-VMGuest -VM $vm.Name -Confirm:$false
while($vm.ExtensionData.Runtime.PowerState -ne 'poweredOff'){
Start-Sleep -Seconds 1
$vm.ExtensionData.UpdateViewData("Runtime.Powerstate")
}
Write-Host "Starting back the VM $($vm.Name) after applying updates"
Start-VM -VM $vm.Name -Confirm:$false
$vm.ExtensionData.UpdateViewData("Guest.GuestOperationsReady")
while($vm.ExtensionData.Guest.GuestOperationsReady -ne "True"){
Start-Sleep -Seconds 1
$vm.ExtensionData.UpdateViewData("Guest.GuestOperationsReady")
}
Write-Host "Performing Final Stop operation on VM $($vm.Name)"
Stop-VMGuest -VM $vm.Name -Confirm:$false
$vm.ExtensionData.UpdateViewData("Runtime.PowerState")
while($vm.ExtensionData.Runtime.PowerState -eq 'poweredOn'){
Start-Sleep -Seconds 1
$vm.ExtensionData.UpdateViewData("Runtime.Powerstate")
}
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.