I am sorry if my question is not clear. Let me explain again.
I am not trying to check updates are installed or not by using invoke operation. I just rebooting a set of vms to apply updates once done then powering off the VM.
When 3rd line of the script is executed the VM will shutdown and in console I can see updating windows don't shutdown. So my while condition will wait till powerstate becomes poweredOff --(Till here working fine)
When 9th line of the script is executed the VM will power on and when it is booting up. On the console it says windows is applying the updates but the Guest.GuestOperationsReady property shows true so the script will not wait in whileloop as expected and it will go to 16th line to stop the VM. But actual vm is still applying updates.
So any other option instead of checking Guest.GuestOperationsReady property after line 9 (Something like if vm console shows ctrl+alt+del screen) then go to line 16th to stop the vm.
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")
}
}