Update check-uptime.ps1

This commit is contained in:
Markus Fleschutz 2023-10-11 08:10:02 +02:00
parent febe6ed9f7
commit 1b48f27d40

View File

@ -2,7 +2,7 @@
.SYNOPSIS .SYNOPSIS
Checks the uptime Checks the uptime
.DESCRIPTION .DESCRIPTION
This PowerShell script queries the computer's uptime and prints it. This PowerShell script queries the computer's uptime (time between now and last boot up time) and prints it.
.EXAMPLE .EXAMPLE
PS> ./check-uptime.ps1 PS> ./check-uptime.ps1
Up for 2 days, 20 hours, 10 minutes Up for 2 days, 20 hours, 10 minutes
@ -14,33 +14,33 @@
try { try {
if ($IsLinux) { if ($IsLinux) {
$Uptime = (Get-Uptime) $uptime = (Get-Uptime)
} else { } else {
$BootTime = Get-WinEvent -ProviderName eventlog | Where-Object {$_.Id -eq 6005} | Select-Object TimeCreated -First 1 $lastBootTime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$Uptime = New-TimeSpan -Start $BootTime.TimeCreated.Date -End (Get-Date) $uptime = New-TimeSpan -Start $lastBootTime -End (Get-Date)
} }
$Reply = "✅ Up for " $reply = "✅ Up for "
$Days = $Uptime.Days $days = $uptime.Days
if ($Days -eq "1") { if ($days -eq "1") {
$Reply += "1 day, " $reply += "1 day, "
} elseif ($Days -ne "0") { } elseif ($days -ne "0") {
$Reply += "$Days days, " $reply += "$days days, "
} }
$Hours = $Uptime.Hours $hours = $uptime.Hours
if ($Hours -eq "1") { if ($hours -eq "1") {
$Reply += "1 hour, " $reply += "1 hour, "
} elseif ($Hours -ne "0") { } elseif ($hours -ne "0") {
$Reply += "$Hours hours, " $reply += "$hours hours, "
} }
$Minutes = $Uptime.Minutes $minutes = $uptime.Minutes
if ($Minutes -eq "1") { if ($minutes -eq "1") {
$Reply += "1 minute" $reply += "1 minute"
} else { } else {
$Reply += "$Minutes minutes" $reply += "$minutes minutes"
} }
Write-Host $Reply Write-Host $reply
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"