PowerShell/Scripts/check-pending-reboot.ps1

73 lines
3.0 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
2023-06-13 19:33:24 +02:00
This PowerShell script queries pending operating system reboots and prints it.
2022-10-04 16:09:27 +02:00
.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
}
2023-01-01 19:22:35 +01:00
try {
$Reason = ""
2023-01-13 17:59:15 +01:00
if ($IsLinux) {
if (Test-Path "/var/run/reboot-required") {
2023-07-14 12:45:50 +02:00
$Reason = "found: /var/run/reboot-required"
2023-07-05 12:11:13 +02:00
Write-Host "⚠️ Pending reboot ($Reason)"
2023-01-13 17:59:15 +01:00
}
} else {
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") {
2023-06-13 19:33:24 +02:00
$Reason += ", ...\Auto Update\RebootRequired"
2023-01-13 17:59:15 +01:00
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting") {
2023-06-13 19:33:24 +02:00
$Reason += ", ...\Auto Update\PostRebootReporting"
2023-01-13 17:59:15 +01:00
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
2023-06-13 19:33:24 +02:00
$Reason += ", ...\Component Based Servicing\RebootPending"
2023-01-13 17:59:15 +01:00
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\ServerManager\CurrentRebootAttempts") {
2023-06-13 19:33:24 +02:00
$Reason += ", ...\ServerManager\CurrentRebootAttempts"
2023-01-13 17:59:15 +01:00
}
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "RebootInProgress") {
2023-06-13 19:33:24 +02:00
$Reason += ", ...\CurrentVersion\Component Based Servicing with 'RebootInProgress'"
2023-01-13 17:59:15 +01:00
}
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "PackagesPending") {
2023-02-23 11:39:35 +01:00
$Reason += ", '...\CurrentVersion\Component Based Servicing' with 'PackagesPending'"
2023-01-13 17:59:15 +01:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Value "PendingFileRenameOperations2") {
2023-02-23 11:39:35 +01:00
$Reason += ", '...\CurrentControlSet\Control\Session Manager' with 'PendingFileRenameOperations2'"
2023-01-13 17:59:15 +01:00
}
if (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Value "DVDRebootSignal") {
2023-02-23 11:39:35 +01:00
$Reason += ", '...\Windows\CurrentVersion\RunOnce' with 'DVDRebootSignal'"
2023-01-13 17:59:15 +01:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "JoinDomain") {
2023-02-23 11:39:35 +01:00
$Reason += ", '...\CurrentControlSet\Services\Netlogon' with 'JoinDomain'"
2023-01-13 17:59:15 +01:00
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "AvoidSpnSet") {
2023-02-23 11:39:35 +01:00
$Reason += ", '...\CurrentControlSet\Services\Netlogon' with 'AvoidSpnSet'"
2023-01-13 17:59:15 +01:00
}
2023-07-05 12:11:13 +02:00
if ($Reason -ne "") {
Write-Host "⚠️ Pending reboot (registry contains $($Reason.substring(2)))"
}
2023-01-01 19:22:35 +01:00
}
2023-07-05 12:11:13 +02:00
if ($Reason -eq "") {
2023-01-01 19:22:35 +01:00
Write-Host "✅ No pending reboot"
}
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2023-07-05 12:11:13 +02:00
}