Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell for checking reboot status after installing windows updates

Here is the code i am using for searching Windows updates installed by WSUS, I want to add one more column for the status of reboot pending/done. Is there a switch for that?

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

$Searcher = $Session.CreateUpdateSearcher()

$historyCount = $Searcher.GetTotalHistoryCount()

$Searcher.QueryHistory(0, $historyCount) | Select-Object Date,

   @{name="Operation"; expression={switch($_.operation){

       1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}},

   @{name="Status"; expression={switch($_.resultcode){

       1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"};

       4 {"Failed"}; 5 {"Aborted"}

}}}, Title | Out-GridView
like image 910
Root Loop Avatar asked Feb 19 '26 17:02

Root Loop


1 Answers

A brief look at the COM object properties and methods does not show anything for this. You can query update before to see if they might trigger a reboot but it is not a guarantee of how the client will react.

There might be other ways but if you want to know for sure of the current state one suggestion would be to look in the registry.

If a patch was installed by WindowsUpdates that requires a reboot it should leave a registry entry in this location:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired

So you just need to check if there are any values in that key to know its pending state as far as WU is concerned.

$pendingRebootKey = "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"
$results = (Get-Item $pendingRebootKey -ErrorAction SilentlyContinue).Property

if($results){
    # Reboot is pending
}

Using -ErrorAction is useful since, according to the article:

Note that the RebootRequired key is automatically deleted when the machine reboots as it's volatile (only held in memory).

This could hide other potential issues so you might need to change the logic to a try/catch and look at the specific error for things like ItemNotFoundException if there is concern there.

like image 165
Matt Avatar answered Feb 25 '26 01:02

Matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!