Skip to content

Instantly share code, notes, and snippets.

@meoso
Created June 28, 2017 13:04
Show Gist options
  • Save meoso/e5b18b318448d28733e87b768655bbe5 to your computer and use it in GitHub Desktop.
Save meoso/e5b18b318448d28733e87b768655bbe5 to your computer and use it in GitHub Desktop.
Powershell function to determine if machine requires reboot.
<#
Powershell function to determine if machine requires reboot.
by Michael Simmons : http://ilovepowershell.com/2015/09/10/how-to-check-if-a-server-needs-a-reboot/
Adapted from https://gist.github.com/altrive/5329377
Based on <http://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542>
#>
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment