Update check-uptime.ps1

This commit is contained in:
Markus Fleschutz 2023-10-17 21:54:47 +02:00
parent d3b59ef7eb
commit 69e0b11c69

View File

@ -12,35 +12,29 @@
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
function TimeSpan2String([TimeSpan]$uptime)
{
[int]$days = $uptime.Days
[int]$hours = $days * 24 + $uptime.Hours
if ($days -gt 2) {
return "$days days"
} elseif ($hours -gt 1) {
return "$hours hours"
} else {
return "$($uptime.Minutes)min"
}
}
try { try {
if ($IsLinux) { if ($IsLinux) {
$uptime = (Get-Uptime) $uptime = (Get-Uptime)
Write-Host "✅ Up for $(TimeSpan2String $uptime)"
} else { } else {
[system.threading.thread]::currentthread.currentculture = [system.globalization.cultureinfo]"en-US"
$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 "✅ Up for $(TimeSpan2String $uptime) since $($lastBootTime.ToShortDateString())"
} }
$reply = "✅ Up for "
$days = $uptime.Days
if ($days -eq "1") {
$reply += "1 day, "
} elseif ($days -ne "0") {
$reply += "$days days, "
}
$hours = $uptime.Hours
if ($hours -eq "1") {
$reply += "1 hour, "
} elseif ($hours -ne "0") {
$reply += "$hours hours, "
}
$minutes = $uptime.Minutes
if ($minutes -eq "1") {
$reply += "1 minute"
} else {
$reply += "$minutes minutes"
}
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])"