Skip to main content

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 = KUInt32] "0x80000002"
$WMI_Reg = RWMIClass] "\\$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
}

 

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