Updated check-uptime.ps1

This commit is contained in:
Markus Fleschutz 2024-02-21 16:32:16 +01:00
parent 306cd65327
commit 1e6d5ac3a5

View File

@ -12,7 +12,7 @@
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
function TimeSpan2String([TimeSpan]$uptime) function TimeSpanAsString([TimeSpan]$uptime)
{ {
[int]$days = $uptime.Days [int]$days = $uptime.Days
[int]$hours = $days * 24 + $uptime.Hours [int]$hours = $days * 24 + $uptime.Hours
@ -26,16 +26,15 @@ function TimeSpan2String([TimeSpan]$uptime)
} }
try { try {
$hostname = $(hostname)
if ($IsLinux) {
$uptime = (Get-Uptime)
Write-Host "$hostname is up for $(TimeSpan2String $uptime)"
} else {
[system.threading.thread]::currentthread.currentculture = [system.globalization.cultureinfo]"en-US" [system.threading.thread]::currentthread.currentculture = [system.globalization.cultureinfo]"en-US"
if ($IsLinux) {
$lastBootTime = (Get-Uptime -since)
$uptime = (Get-Uptime)
} else {
$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 $(TimeSpan2String $uptime) since $($lastBootTime.ToShortDateString())"
} }
Write-Host "$(hostname) is up for $(TimeSpanAsString $uptime) since $($lastBootTime.ToShortDateString())"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"