How to check if any Windows server is pending for reboot (PowerShell)
Introduction
This PowerShell script will check if a server is pending for reboot and get the reboot status.
Scenarios
IT admins might log onto a server and wonder if there have been patches installed and anything needs to be rebooted. This is important to know because admins will not be able to install updates if the system is pending a reboot from a previous software update.
Script
Step1: Start the PowerShell Console with administrator. To run the script in the Windows PowerShell Console, type the command< Script Path> at the Windows PowerShell Console.
For example, type C:\Script\FindServerIsPendingReboot.ps1
The step is shown in the following figure.
When the script finishes running, it will display the results as shown below.
Here are some code snippets for your reference.
#Determine PendingFileRenameOperations exists of not
$PendFileKeyPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\"
Invoke-Command -ComputerName $cn -ScriptBlock{
Get-ItemProperty -Path $using:PendFileKeyPath -name PendingFileRenameOperations} |`
Foreach{If($_.PendingFileRenameOperations){$PendingFile = $true}Else{$PendingFile = $false}}
#Determine RebootRequired subkey exists or not
$AutoUpdateKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
Invoke-Command -ComputerName $cn -ScriptBlock {Test-Path -Path "$using:AutoUpdateKeyPath\RebootRequired"} |`
Foreach{If($_ -eq $true){$AutoUpdate = $true}Else{$AutoUpdate = $false}}
#Determine PendingFileRenameOperations exists of not $PendFileKeyPath = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\" Invoke-Command -ComputerName $cn -ScriptBlock{ Get-ItemProperty -Path $using:PendFileKeyPath -name PendingFileRenameOperations} |` Foreach{If($_.PendingFileRenameOperations){$PendingFile = $true}Else{$PendingFile = $false}} #Determine RebootRequired subkey exists or not $AutoUpdateKeyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" Invoke-Command -ComputerName $cn -ScriptBlock {Test-Path -Path "$using:AutoUpdateKeyPath\RebootRequired"} |` Foreach{If($_ -eq $true){$AutoUpdate = $true}Else{$AutoUpdate = $false}}
Windows PowerShell 3.0
Windows 7 or later version
Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.