vin01
Expert
Expert

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

Jump to solution

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")

}

}

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

The GuestOperationsReady property tells you that GuestOperations are ready.

From then on you can for example use Invoke-VMScript.

To check if all Updates are installed, I would check via the CreateUpdateSearcher.

Run the following at intervals, till the count is zero.

$updateObject = New-Object -ComObject Microsoft.Update.Session

$updateSearcher = $updateObject.CreateUpdateSearcher()

$searchResults = $updateSearcher.Search("IsInstalled=0")

Write-Host $searchResults.Updates.Count


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership

The GuestOperationsReady property tells you that GuestOperations are ready.

From then on you can for example use Invoke-VMScript.

To check if all Updates are installed, I would check via the CreateUpdateSearcher.

Run the following at intervals, till the count is zero.

$updateObject = New-Object -ComObject Microsoft.Update.Session

$updateSearcher = $updateObject.CreateUpdateSearcher()

$searchResults = $updateSearcher.Search("IsInstalled=0")

Write-Host $searchResults.Updates.Count


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

View solution in original post

0 Kudos
vin01
Expert
Expert

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")

}

}

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership

The question is clear.

After the restart the updates are being applied.
The GuestOperationsReady becomes $true, at that point you can use the code I posted to check if there are still updates that are not applied yet.


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

0 Kudos
vin01
Expert
Expert

Thanks Its working. I used as below as scripttext in invoke-vmscript.

$updateObject = New-Object -ComObject Microsoft.Update.Session

$updateSearcher = $updateObject.CreateUpdateSearcher()

$searchResults = $updateSearcher.Search("IsInstalled=0")

$timeoutValue = 1200

$startTime = Get-Date

while($searchResults.Updates.Count -ne '0' -and (New-TimeSpan -Start $startTime -End (Get-Date)).TotalSeconds -lt $timeoutValue){

Start-Sleep 1

$searchResults.Updates.Count

}

if((New-TimeSpan -Start $startTime -End (Get-Date)).TotalSeconds -ge $timeoutValue) {

'windows update completed Partially'

}

else{

'windows update completed sucessfully'

}

Regards Vineeth.K
0 Kudos