PowerShell/Scripts/check-uptime.ps1

49 lines
1.0 KiB
PowerShell
Raw Normal View History

2021-11-24 14:59:01 +01:00
<#
2021-11-24 14:56:38 +01:00
.SYNOPSIS
Checks the uptime
2021-11-24 14:56:38 +01:00
.DESCRIPTION
2023-10-11 08:10:02 +02:00
This PowerShell script queries the computer's uptime (time between now and last boot up time) and prints it.
2021-11-24 14:56:38 +01:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-uptime.ps1
Up for 2 days, 20 hours, 10 minutes
2021-11-24 14:56:38 +01:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
2021-11-24 14:56:38 +01:00
#>
try {
if ($IsLinux) {
2023-10-11 08:10:02 +02:00
$uptime = (Get-Uptime)
2021-11-24 14:56:38 +01:00
} else {
2023-10-11 08:10:02 +02:00
$lastBootTime = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$uptime = New-TimeSpan -Start $lastBootTime -End (Get-Date)
2021-11-27 14:29:49 +01:00
}
2023-10-11 08:10:02 +02:00
$reply = "✅ Up for "
$days = $uptime.Days
if ($days -eq "1") {
$reply += "1 day, "
} elseif ($days -ne "0") {
$reply += "$days days, "
2021-11-27 14:29:49 +01:00
}
2023-10-11 08:10:02 +02:00
$hours = $uptime.Hours
if ($hours -eq "1") {
$reply += "1 hour, "
} elseif ($hours -ne "0") {
$reply += "$hours hours, "
2021-11-27 14:29:49 +01:00
}
2023-10-11 08:10:02 +02:00
$minutes = $uptime.Minutes
if ($minutes -eq "1") {
$reply += "1 minute"
2022-10-21 18:05:08 +02:00
} else {
2023-10-11 08:10:02 +02:00
$reply += "$minutes minutes"
2021-11-27 14:29:49 +01:00
}
2023-10-11 08:10:02 +02:00
Write-Host $reply
2021-11-24 14:56:38 +01:00
exit 0 # success
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2021-11-24 14:56:38 +01:00
exit 1
2023-08-06 21:35:36 +02:00
}