This is rough, and provided in case it helps anyone. It actually will be part of a larger management script at some point.
$Version = "wsus_rbquery.ps1 v10.6.14 Aaron Dodd"
$Usage = "Usage: wsus_rbquery.ps1 [servers], where [servers] is one or more servers to set up the tasks on (comma-separated if more than one) or a one-server-per line text file ending in .txt"
$Description = "Checks for pending reboots on target server(s)"
# Based on a script found at http://www.techmumbojumblog.com/?p=375&cpage=1
# Expects 1 or more (comma-delimited if more) servers
If (!$Args[0]) {
Write-Host $Version
Write-Host $Usage
Write-Host $Description
Exit
}
$Servers = $Args[0].ToLower()
If ($Servers.Contains(".txt")) {
$Servers = gc $Servers
} ElseIf ($Servers.Contains(",")) {
$Servers = [regex]::Split($Servers,",")
}
function Get-PendingReboot {
param([string]$computer)
$hkey = 'LocalMachine';
$path_server = 'SOFTWARE\Microsoft\ServerManager';
$path_wsus = 'SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired';
$path_control = 'SYSTEM\CurrentControlSet\Control';
$path_session = join-path $path_control 'Session Manager';
$path_name = join-path $path_control 'ComputerName';
$path_name_old = join-path $path_name 'ActiveComputerName';
$path_name_new = join-path $path_name 'ComputerName';
$pending_rename = 'PendingFileRenameOperations';
$pending_rename_2 = 'PendingFileRenameOperations2';
$attempts = 'CurrentRebootAttempts';
$computer_name = 'ComputerName';
$num_attempts = 0;
$name_old = $null;
$name_new = $null;
$reg= [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($hkey, $computer);
$key_session = $reg.OpenSubKey($path_session);
if ($key_session -ne $null) {
$session_values = @($key_session.GetValueNames());
$key_session.Close() | out-null;
}
$key_server = $reg.OpenSubKey($path_server);
if ($key_server -ne $null) {
$num_attempts = $key_server.GetValue($attempts);
$key_server.Close() | out-null;
}
$key_name_old = $reg.OpenSubKey($path_name_old);
if ($key_name_old -ne $null) {
$name_old = $key_name_old.GetValue($computer_name);
$key_name_old.Close() | out-null;
$key_name_new = $reg.OpenSubKey($path_name_new);
if ($key_name_new -ne $null) {
$name_new = $key_name_new.GetValue($computer_name);
$key_name_new.Close() | out-null;
}
}
$key_wsus = $reg.OpenSubKey($path_wsus);
if ($key_wsus -ne $null) {
$wsus_values = @($key_wsus.GetValueNames());
if ($wsus_values) {
$wsus_rbpending = $true
} else {
$wsus_rbpending = $false
}
$key_wsus.Close() | out-null;
}
$reg.Close() | out-null;
if ( `
(($session_values -contains $pending_rename) -or ($session_values -contains $pending_rename_2)) `
-or (($num_attempts -gt 0) -or ($name_old -ne $name_new)) `
-or ($wsus_rbpending)) {
return $true;
}
else {
return $false;
}
}
ForEach ($Server in $servers) {
Write-Host $Server - (Get-PendingReboot $Server)
}