How To Tell If A Remote Computer Needs Reboot

0

Knowing when to reboot your computer is obvious but how to tell if a remote computer needs a reboot? It could be quite useful to a sysadmin to know which server or workstation is pending for a reboot, whether to finish up a regular Windows Update or a new software installation.

image 10 - How To Tell If A Remote Computer Needs Reboot

There are a few registry keys scattered in the system tray to flag a pending reboot. Here are two of them:

The RebootPending key at:

HKLM\Software\Windows\CurrentVersion\Component Based Servicing

And the RebootRequired key under:

HKLM\Software\Windows\CurrentVersion\WindowsUpdate\Auto Update

If any of the keys exists in their respective location, a reboot is needed for that computer. And PowerShell is pretty capable of checking their existence with a single line like below:

Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'

Or

Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'

Returning True means your local computer needs a reboot.

image 8 600x135 - How To Tell If A Remote Computer Needs Reboot

To execute the same cmdlet on a remote computer, you will need help from Invoke-Command, such as:

$command = {Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'}
Invoke-Command -computer ComputerName -ScriptBlock $command
image 9 600x55 - How To Tell If A Remote Computer Needs Reboot

Obviously, in order to have a successful Invoke-Command execution, you need PSRemoting/WinRM enabled on the remote computer.

Binding all together, here is the snippet that you can use to check and tell if a remote computer needs a reboot to finish up what it’s been doing.

$pendingReboot = @(
    @{
        Name = 'Reboot Pending Status: '
        Test = { Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending'}
    }
    @{
        Name = 'Reboot Required by Windows Update: '
        Test = { Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'}
    })
$computername = "ComputerName"
$session = New-PSSession -Computer $computername
foreach ($test in $pendingReboot) {
    $result = Invoke-Command -Session $session -ScriptBlock $test.Test
    $test.Name + $result
}

The result will look like this:

image 11 600x286 - How To Tell If A Remote Computer Needs Reboot

Lastly, thanks to 4sysops for sharing the idea. However, I had trouble using their code so I took a different route to get the same result.

SHARE

LEAVE A REPLY

Please enter your comment!
Please enter your name here