mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-28 17:48:59 +01:00
65 lines
1.6 KiB
Markdown
65 lines
1.6 KiB
Markdown
Script: *check-uptime.ps1*
|
|
========================
|
|
|
|
check-uptime.ps1
|
|
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
|
|
|
|
[<CommonParameters>]
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
```
|
|
|
|
Script Content
|
|
--------------
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Checks the uptime
|
|
.DESCRIPTION
|
|
This PowerShell script queries the computer's uptime (time between now and last boot up time) and prints it.
|
|
.EXAMPLE
|
|
PS> ./check-uptime.ps1
|
|
✅ OfficePC is up for 13 days since 1/25/2024
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
function TimeSpanAsString([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 {
|
|
[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
|
|
$uptime = New-TimeSpan -Start $lastBootTime -End (Get-Date)
|
|
}
|
|
Write-Host "✅ $(hostname) is up for $(TimeSpanAsString $uptime) since $($lastBootTime.ToShortDateString())"
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|
|
```
|
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of check-uptime.ps1 as of 08/15/2024 09:50:46)*
|