Last active
April 20, 2018 09:11
-
-
Save atao/f72d4581087646df8988ad4ac83d0eea to your computer and use it in GitHub Desktop.
Test-PendingReboot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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