Skip to main content

Detecting Windows Update Reboot

  • October 24, 2024
  • 1 reply
  • 109 views

jack.smith
Forum|alt.badge.img+1

I’ve been running a worklet to detect reboots based on

  • Windows Update Pending
  • Uptime exceeds 28 days

Started noticing many workstations pending a reboot were not triggering on the evaluation code. 

$sysInfo = New-Object -ComObject "Microsoft.Update.SystemInfo"

 

While there is a way to likely tune above, I’ve opted to add another strategy. This includes a registry lookup using 32-bit Powershell as this key will need it.

# Query WMI for LastBootUpTIme
$uptime = (get-date) - (gcim Win32_OperatingSystem).LastBootUpTime

# Check Registry for Windows Update Pending Update
$scriptblock = {
    function Get-WUAURebootReq {
        ## Making registry connection to the local/remote computer
        $HKLM = [UInt32] "0x80000002"
        $WMI_Reg = [WMIClass] "\\$ENV:COMPUTERNAME\root\default:StdRegProv"

        ## Query WUAU from the registry
        $RegWUAURebootReq = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
        $WUAURebootReq = $RegWUAURebootReq.sNames -contains "RebootRequired"
        Return $WUAURebootReq
    }
    Get-WUAURebootReq
}
$reboot = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptblock

# Check with COM Object
$sysInfo = New-Object -ComObject "Microsoft.Update.SystemInfo"

if($reboot -eq $true -or $uptime.Days -gt 28 -or $sysInfo.RebootRequired){ 
  exit 1 # reboot is needed
}else{
  exit 0 #no reboot is needed
}

 

1 reply

Forum|alt.badge.img
  • Automox Employee
  • 129 replies
  • October 24, 2024

I’ve used a powershell module called ‘PendingReboot’ to check the exact reason why a device would indicate it needs a restart. I’m sure it could be incorporated into an eval code in some creative fashion as well:
 

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings