PowerShell/docs/check-uptime.md

118 lines
4.1 KiB
Markdown
Raw Permalink Normal View History

2024-11-08 12:38:20 +01:00
The *check-uptime.ps1* Script
===========================
2021-12-09 16:19:09 +01:00
2023-10-19 08:12:00 +02:00
check-uptime.ps1
2021-12-09 16:19:09 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-12-09 16:19:09 +01:00
```powershell
2023-10-19 08:12:00 +02:00
2021-12-09 16:19:09 +01:00
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2023-05-26 12:20:18 +02:00
Checks the uptime
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2023-10-19 08:12:00 +02:00
This PowerShell script queries the computer's uptime (time between now and last boot up time) and prints it.
2022-11-17 20:02:26 +01:00
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./check-uptime.ps1
2024-03-27 17:36:59 +01:00
✅ OfficePC is up for 13 days since 1/25/2024
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-03-27 17:36:59 +01:00
function TimeSpanAsString([TimeSpan]$uptime)
2023-10-19 08:12:00 +02:00
{
[int]$days = $uptime.Days
[int]$hours = $days * 24 + $uptime.Hours
if ($days -gt 2) {
return "$days days"
} elseif ($hours -gt 1) {
return "$hours hours"
2022-11-17 20:02:26 +01:00
} else {
2023-10-19 08:12:00 +02:00
return "$($uptime.Minutes)min"
2022-11-17 20:02:26 +01:00
}
2023-10-19 08:12:00 +02:00
}
2023-05-26 12:20:18 +02:00
2024-11-08 12:35:11 +01:00
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
}
}
2023-10-19 08:12:00 +02:00
try {
2024-03-27 17:36:59 +01:00
[system.threading.thread]::currentthread.currentculture = [system.globalization.cultureinfo]"en-US"
2023-10-19 08:12:00 +02:00
if ($IsLinux) {
2024-03-27 17:36:59 +01:00
$lastBootTime = (Get-Uptime -since)
2023-10-19 08:12:00 +02:00
$uptime = (Get-Uptime)
2022-11-17 20:02:26 +01:00
} else {
2023-10-19 08:12:00 +02:00
$lastBootTime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$uptime = New-TimeSpan -Start $lastBootTime -End (Get-Date)
2022-11-17 20:02:26 +01:00
}
2024-11-08 12:35:11 +01:00
$status = "✅"
$pending = ""
if ($IsLinux) {
if (Test-Path "/var/run/reboot-required") {
$status = "⚠️"
$pending = "with 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 = "with pending reboot ($($reason.substring(2)) in registry)"
}
}
Write-Host "$status $(hostname) is up for $(TimeSpanAsString $uptime) since $($lastBootTime.ToShortDateString()) $pending"
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-11-20 11:52:20 +01:00
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:52)*