Skip to content

Instantly share code, notes, and snippets.

@atao
Last active April 20, 2018 09:11
Show Gist options
  • Save atao/f72d4581087646df8988ad4ac83d0eea to your computer and use it in GitHub Desktop.
Save atao/f72d4581087646df8988ad4ac83d0eea to your computer and use it in GitHub Desktop.
Test-PendingReboot
# http://ilovepowershell.com/2015/09/10/how-to-check-if-a-server-needs-a-reboot/
function Test-PendingReboot
{
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return $true }
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { return $true }
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true }
try {
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
$status = $util.DetermineIfRebootPending()
if(($status -ne $null) -and $status.RebootPending){
return $true
}
}catch{}
return $false
}
If (Test-PendingReboot -eq $true){
Write-Host "Reboot is needed!"
Restart-Computer -Confirm
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment