PowerShell/Scripts/check-pending-reboot.ps1

63 lines
3.3 KiB
PowerShell
Raw Normal View History

2022-10-09 19:16:32 +02:00
<#
2022-10-04 16:09:27 +02:00
.SYNOPSIS
Check for pending reboots
.DESCRIPTION
This PowerShell script checks different registry keys and values to determine if a reboot is pending.
.EXAMPLE
./check-pending-reboot.ps1
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
function Test-RegistryValue { param([parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]$Path, [parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]$Value)
try {
Get-ItemProperty -Path $Path -Name $Value -EA Stop
return $true
} catch {
return $false
}
}
2022-10-09 12:34:06 +02:00
$Reason = ""
2022-10-04 16:09:27 +02:00
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") {
2022-10-11 19:44:34 +02:00
$Reason += ", found registry key: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"
2022-10-04 16:09:27 +02:00
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting") {
2022-10-11 19:44:34 +02:00
$Reason += ", found registry key: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting"
2022-10-04 16:09:27 +02:00
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
2022-10-11 19:44:34 +02:00
$Reason += ", found registry key: HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending"
2022-10-04 16:09:27 +02:00
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\ServerManager\CurrentRebootAttempts") {
2022-10-11 19:44:34 +02:00
$Reason += ", found registry key: HKLM:\SOFTWARE\Microsoft\ServerManager\CurrentRebootAttempts"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "RebootInProgress") {
2022-10-11 19:44:34 +02:00
$Reason += ", registry key 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing' contains: RebootInProgress"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "PackagesPending") {
2022-10-11 19:44:34 +02:00
$Reason += ", registry key 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing' contains: PackagesPending"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Value "PendingFileRenameOperations") {
2022-10-11 19:44:34 +02:00
$Reason += ", registry key 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' contains: PendingFileRenameOperations"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Value "PendingFileRenameOperations2") {
2022-10-11 19:44:34 +02:00
$Reason += ", registry key 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' contains: PendingFileRenameOperations2"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Value "DVDRebootSignal") {
2022-10-11 19:44:34 +02:00
$Reason += ", registry key 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce' contains: DVDRebootSignal"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "JoinDomain") {
2022-10-11 19:44:34 +02:00
$Reason += ", registry key 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon' contains: JoinDomain"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "AvoidSpnSet") {
2022-10-11 19:44:34 +02:00
$Reason += ", registry key 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon' contains: AvoidSpnSet"
2022-10-09 12:34:06 +02:00
}
if ($Reason -eq "") {
"✅ No pending reboot."
} else {
2022-10-11 19:44:34 +02:00
"⚠️ Pending reboot$Reason."
2022-10-04 16:09:27 +02:00
}
exit 0 # success