Root Loop
Root Loop

Reputation: 3162

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

Upvotes: 3

Views: 5736

Answers (1)

Matt
Matt

Reputation: 46730

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.

Upvotes: 3

Related Questions