Chirishman bio photo

Chirishman

Sysadmin with an interest in PowerShell, automation, SBCs, and Datavis.

Connect

@the_chirishman Github Stackoverflow RSS Feed

My GitHub Repos

ADAuth Keystore PowerArchiver Powershell-SlackBot StoredPSCredential

Overview

Intro

Here is a self contained snippet of code which I’ve assembled to easily check whether a reboot is pending on a given machine for any reason. The list of check locations is based on/captured from the xPendingReboot DSC resource. Full script is at the end.

Scripts

Initialize a container for the output

$RebootCheck = @{}

Check for pending Component Based Servicing and store the result

$ComponentBasedServicingKeys = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\').Name

$RebootCheck['ComponentBasedServicing'] = $ComponentBasedServicingKeys -Split "\\" -contains "RebootPending"

Check for a pending Windows Update reboot

$WindowsUpdateKeys = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\').Name

$RebootCheck['WindowsUpdate'] = $WindowsUpdateKeys -Split "\\" -contains "RebootRequired"

Check for pending file rename

$RebootCheck['PendingFileRename'] = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\').PendingFileRenameOperations.Length -gt 0

Check for pending Computer Rename

$ActiveComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName').ComputerName
$PendingComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName').ComputerName
$RebootCheck['PendingComputerRename'] = $ActiveComputerName -ne $PendingComputerName

Return Reboot Check Results

$RebootCheck

Complete Script

$RebootCheck = @{}

$ComponentBasedServicingKeys = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\').Name

$RebootCheck['ComponentBasedServicing'] = $ComponentBasedServicingKeys -Split "\\" -contains "RebootPending"

$WindowsUpdateKeys = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\').Name

$RebootCheck['WindowsUpdate'] = $WindowsUpdateKeys -Split "\\" -contains "RebootRequired"

$RebootCheck['PendingFileRename'] = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\').PendingFileRenameOperations.Length -gt 0
$ActiveComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName').ComputerName
$PendingComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName').ComputerName
$RebootCheck['PendingComputerRename'] = $ActiveComputerName -ne $PendingComputerName

$RebootCheck