Improved some scripts

This commit is contained in:
Markus Fleschutz 2024-09-09 22:03:20 +02:00
parent b0148fcc53
commit 8f7237196c
3 changed files with 71 additions and 21 deletions

View File

@ -22,50 +22,48 @@ function Test-RegistryValue { param([parameter(Mandatory=$true)][ValidateNotNull
} }
try { try {
$Reason = "" $reply = "✅ No pending reboot"
if ($IsLinux) { if ($IsLinux) {
if (Test-Path "/var/run/reboot-required") { if (Test-Path "/var/run/reboot-required") {
$Reason = "found: /var/run/reboot-required" $reply = "⚠️ Pending reboot (found: /var/run/reboot-required)"
Write-Host "⚠️ Pending reboot ($Reason)"
} }
} else { } else {
$reason = ""
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") { if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") {
$Reason += ", ...\Auto Update\RebootRequired" $reason += ", ...\Auto Update\RebootRequired"
} }
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting") { if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting") {
$Reason += ", ...\Auto Update\PostRebootReporting" $reason += ", ...\Auto Update\PostRebootReporting"
} }
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") { if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
$Reason += ", ...\Component Based Servicing\RebootPending" $reason += ", ...\Component Based Servicing\RebootPending"
} }
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\ServerManager\CurrentRebootAttempts") { if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\ServerManager\CurrentRebootAttempts") {
$Reason += ", ...\ServerManager\CurrentRebootAttempts" $reason += ", ...\ServerManager\CurrentRebootAttempts"
} }
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "RebootInProgress") { if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "RebootInProgress") {
$Reason += ", ...\CurrentVersion\Component Based Servicing with 'RebootInProgress'" $reason += ", ...\CurrentVersion\Component Based Servicing with 'RebootInProgress'"
} }
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "PackagesPending") { if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "PackagesPending") {
$Reason += ", '...\CurrentVersion\Component Based Servicing' with 'PackagesPending'" $reason += ", '...\CurrentVersion\Component Based Servicing' with 'PackagesPending'"
} }
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Value "PendingFileRenameOperations2") { if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Value "PendingFileRenameOperations2") {
$Reason += ", '...\CurrentControlSet\Control\Session Manager' with 'PendingFileRenameOperations2'" $reason += ", '...\CurrentControlSet\Control\Session Manager' with 'PendingFileRenameOperations2'"
} }
if (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Value "DVDRebootSignal") { if (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Value "DVDRebootSignal") {
$Reason += ", '...\Windows\CurrentVersion\RunOnce' with 'DVDRebootSignal'" $reason += ", '...\Windows\CurrentVersion\RunOnce' with 'DVDRebootSignal'"
} }
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "JoinDomain") { if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "JoinDomain") {
$Reason += ", '...\CurrentControlSet\Services\Netlogon' with 'JoinDomain'" $reason += ", '...\CurrentControlSet\Services\Netlogon' with 'JoinDomain'"
} }
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "AvoidSpnSet") { if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "AvoidSpnSet") {
$Reason += ", '...\CurrentControlSet\Services\Netlogon' with 'AvoidSpnSet'" $reason += ", '...\CurrentControlSet\Services\Netlogon' with 'AvoidSpnSet'"
} }
if ($Reason -ne "") { if ($reason -ne "") {
Write-Host "⚠️ Pending reboot (registry got $($Reason.substring(2)))" $reply = "⚠️ Pending reboot (registry got $($reason.substring(2)))"
} }
} }
if ($Reason -eq "") { Write-Host $reply
Write-Host "✅ No pending reboot"
}
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"

View File

@ -22,5 +22,4 @@ Write-Host "`n S O F T W A R E" -foregroundColor green
& "$PSScriptRoot/check-powershell.ps1" & "$PSScriptRoot/check-powershell.ps1"
& "$PSScriptRoot/check-time-zone.ps1" & "$PSScriptRoot/check-time-zone.ps1"
& "$PSScriptRoot/check-swap-space.ps1" & "$PSScriptRoot/check-swap-space.ps1"
& "$PSScriptRoot/check-pending-reboot.ps1" exit 0 # success
exit 0 # success

View File

@ -25,6 +25,15 @@ function TimeSpanAsString([TimeSpan]$uptime)
} }
} }
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
}
}
try { try {
[system.threading.thread]::currentthread.currentculture = [system.globalization.cultureinfo]"en-US" [system.threading.thread]::currentthread.currentculture = [system.globalization.cultureinfo]"en-US"
if ($IsLinux) { if ($IsLinux) {
@ -34,7 +43,51 @@ try {
$lastBootTime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime $lastBootTime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$uptime = New-TimeSpan -Start $lastBootTime -End (Get-Date) $uptime = New-TimeSpan -Start $lastBootTime -End (Get-Date)
} }
Write-Host "$(hostname) is up for $(TimeSpanAsString $uptime) since $($lastBootTime.ToShortDateString())" $status = ""
$pending = "No pending reboot"
if ($IsLinux) {
if (Test-Path "/var/run/reboot-required") {
$status = "⚠️ "
$pending = "Pending reboot (found: /var/run/reboot-required)"
}
} else {
$reason = ""
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired") {
$reason += ", ...\Auto Update\RebootRequired"
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting") {
$reason += ", ...\Auto Update\PostRebootReporting"
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending") {
$reason += ", ...\Component Based Servicing\RebootPending"
}
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\ServerManager\CurrentRebootAttempts") {
$reason += ", ...\ServerManager\CurrentRebootAttempts"
}
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "RebootInProgress") {
$reason += ", ...\CurrentVersion\Component Based Servicing with 'RebootInProgress'"
}
if (Test-RegistryValue -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Value "PackagesPending") {
$reason += ", '...\CurrentVersion\Component Based Servicing' with 'PackagesPending'"
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Value "PendingFileRenameOperations2") {
$reason += ", '...\CurrentControlSet\Control\Session Manager' with 'PendingFileRenameOperations2'"
}
if (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Value "DVDRebootSignal") {
$reason += ", '...\Windows\CurrentVersion\RunOnce' with 'DVDRebootSignal'"
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "JoinDomain") {
$reason += ", '...\CurrentControlSet\Services\Netlogon' with 'JoinDomain'"
}
if (Test-RegistryValue -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon" -Value "AvoidSpnSet") {
$reason += ", '...\CurrentControlSet\Services\Netlogon' with 'AvoidSpnSet'"
}
if ($reason -ne "") {
$status = "⚠️ "
$pending = "Pending reboot (registry got $($reason.substring(2)))"
}
}
Write-Host "$status $(hostname) is up for $(TimeSpanAsString $uptime) since $($lastBootTime.ToShortDateString()) - $pending"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"