Skip to content

Instantly share code, notes, and snippets.

@JonathonAnderson
Created September 4, 2019 15:46
Show Gist options
  • Save JonathonAnderson/a698bf79f60284fce7bd49355b9e9acc to your computer and use it in GitHub Desktop.
Save JonathonAnderson/a698bf79f60284fce7bd49355b9e9acc to your computer and use it in GitHub Desktop.
$PendingReboot = Invoke-Command -ComputerName $Target -ScriptBlock `
{
#Checks if the registry key RebootRequired is present. It is created when Windows Updates are applied and require a reboot to take place
$PatchReboot = Get-ChildItem -Path REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue
#Checks if the RebootPending key is present. It is created when changes are made to the component store that require a reboot to take place
$ComponentBasedReboot = Get-ChildItem -Path REGISTRY::"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -ErrorAction SilentlyContinue
#Checks if File rename operations are taking place and require a reboot for the operation to take effect
$PendingFileRenameOperations = (Get-ItemProperty -Path REGISTRY::"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" -ErrorAction SilentlyContinue).PendingFileRenameOperations
#Performs a WMI query of the configuration manager service to check if a reboot is pending
$ConfigurationManagerReboot = Invoke-WmiMethod -Namespace "ROOT\ccm\ClientSDK" -Class CCM_ClientUtilities -Name DetermineIfRebootPending | select-object -ExpandProperty "RebootPending"
If (($PatchReboot -eq $null) -and ($ComponentBasedReboot -eq $null) -and ($PendingFileRenameOperations -eq $null) -and ($ConfigurationManagerReboot -eq $false)) {
Return $false
} else {
Return $true
}
}
if($PendingReboot)
{
Invoke-Command -ComputerName $Target -ScriptBlock { Restart-Computer -Force }
Remove-Job -Id $Computer.ID
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment