PowerShell/Scripts/check-pending-reboot.ps1

64 lines
3.1 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)
2022-10-11 20:13:04 +02:00
try {
Get-ItemProperty -Path $Path -Name $Value -EA Stop
return $true
} catch {
return $false
}
2022-10-04 16:09:27 +02:00
}
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-11-08 21:06:30 +01:00
$Reason += ", found registry entry '...\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-11-08 21:06:30 +01:00
$Reason += ", found registry entry '...\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-11-08 21:06:30 +01:00
$Reason += ", found registry entry '...\Component Based Servicing\RebootPending'"
2022-10-04 16:09:27 +02:00
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\ServerManager\CurrentRebootAttempts") {
2022-11-08 21:06:30 +01:00
$Reason += ", found registry entry '...\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-11-08 21:06:30 +01:00
$Reason += ", found registry entry '...\CurrentVersion\Component Based Servicing' with 'RebootInProgress'"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "PackagesPending") {
2022-11-08 21:06:30 +01:00
$Reason += ", found registry entry '...\CurrentVersion\Component Based Servicing' with 'PackagesPending'"
2022-10-04 16:09:27 +02:00
}
2022-10-11 20:20:42 +02:00
#if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Value "PendingFileRenameOperations") {
2022-10-29 17:34:15 +02:00
# $Reason += ", found registry entry '...\CurrentControlSet\Control\Session Manager' with 'PendingFileRenameOperations'"
2022-10-11 20:20:42 +02:00
#}
2022-10-04 16:09:27 +02:00
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Value "PendingFileRenameOperations2") {
2022-10-29 17:34:15 +02:00
$Reason += ", found registry entry '...\CurrentControlSet\Control\Session Manager' with 'PendingFileRenameOperations2'"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Value "DVDRebootSignal") {
2022-10-29 17:34:15 +02:00
$Reason += ", found registry entry '...\Windows\CurrentVersion\RunOnce' with 'DVDRebootSignal'"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "JoinDomain") {
2022-10-29 17:34:15 +02:00
$Reason += ", found registry entry '...\CurrentControlSet\Services\Netlogon' with 'JoinDomain'"
2022-10-04 16:09:27 +02:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "AvoidSpnSet") {
2022-10-29 17:34:15 +02:00
$Reason += ", found registry entry '...\CurrentControlSet\Services\Netlogon' with 'AvoidSpnSet'"
2022-10-09 12:34:06 +02:00
}
2022-10-24 11:43:54 +02:00
if ($Reason -ne "") {
"⚠️ Pending reboot ($($Reason.substring(2)))"
2022-10-09 12:34:06 +02:00
} else {
"✅ No pending reboot"
2022-10-04 16:09:27 +02:00
}
exit 0 # success