Updaate check-battery.ps1

This commit is contained in:
Markus Fleschutz 2023-08-05 10:52:16 +02:00
parent ba00df2878
commit d59d656366

View File

@ -5,7 +5,7 @@
This PowerShell script queries the status of the system battery and prints it. This PowerShell script queries the status of the system battery and prints it.
.EXAMPLE .EXAMPLE
PS> ./check-battery PS> ./check-battery
21% battery life, 54 min remaining Battery at 21%, 54 min remaining
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -20,28 +20,25 @@ try {
$Details = [System.Windows.Forms.SystemInformation]::PowerStatus $Details = [System.Windows.Forms.SystemInformation]::PowerStatus
[int]$Percent = 100 * $Details.BatteryLifePercent [int]$Percent = 100 * $Details.BatteryLifePercent
[int]$Remaining = $Details.BatteryLifeRemaining / 60 [int]$Remaining = $Details.BatteryLifeRemaining / 60
switch ($Details.PowerLineStatus) { if ($Details.PowerLineStatus -eq "Online") {
"Online" {
if ($Details.BatteryChargeStatus -eq "NoSystemBattery") { if ($Details.BatteryChargeStatus -eq "NoSystemBattery") {
$Reply = "✅ AC powered" $Reply = "✅ AC powered"
} elseif ($Percent -eq 100) { } elseif ($Percent -ge 95) {
$Reply = "✅ Battery $Percent% full" $Reply = "✅ Battery fully charged ($Percent%)"
} else { } else {
$Reply = "✅ Battery $Percent%, charging..." $Reply = "✅ Battery charging... ($Percent%)"
} }
} } else { # must be offline
"Offline" { if ($Percent -ge 80) {
if ($Percent -eq 100) { $Reply = "✅ Battery $Percent% full, $Remaining min remaining"
$Reply = "$Percent% full battery, $Remaining min remaining"
} elseif ($Remaining -gt 30) { } elseif ($Remaining -gt 30) {
$Reply = "$Percent% battery life, $Remaining min remaining" $Reply = "Battery at $Percent%, $Remaining min remaining"
} else { } else {
$Reply = "⚠️ $Percent% battery life, only $Remaining min remaining" $Reply = "⚠️ Battery at $Percent%, only $Remaining min remaining"
} }
} }
}
} }
Write-Host $Reply Write-Output $Reply
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"