PowerShell/Scripts/check-battery.ps1

53 lines
1.6 KiB
PowerShell
Raw Normal View History

2022-11-30 08:42:00 +01:00
<#
.SYNOPSIS
2023-01-21 19:09:01 +01:00
Checks the battery
2022-11-30 08:42:00 +01:00
.DESCRIPTION
2023-01-21 19:09:01 +01:00
This PowerShell script queries the status of the system battery and prints it.
2022-11-30 08:42:00 +01:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./check-battery.ps1
2023-08-05 12:10:16 +02:00
Battery 9% low, 54 min remaining
2022-11-30 08:42:00 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
if ($IsLinux) {
2023-01-21 19:09:01 +01:00
$Reply = "✅ AC powered" # TODO, just guessing :-)
} else {
Add-Type -Assembly System.Windows.Forms
$Details = [System.Windows.Forms.SystemInformation]::PowerStatus
2023-01-21 19:09:01 +01:00
[int]$Percent = 100 * $Details.BatteryLifePercent
[int]$Remaining = $Details.BatteryLifeRemaining / 60
2023-08-05 10:52:16 +02:00
if ($Details.PowerLineStatus -eq "Online") {
2023-01-21 19:09:01 +01:00
if ($Details.BatteryChargeStatus -eq "NoSystemBattery") {
$Reply = "✅ AC powered"
2023-08-05 10:52:16 +02:00
} elseif ($Percent -ge 95) {
$Reply = "✅ Battery fully charged ($Percent%)"
2023-01-21 19:09:01 +01:00
} else {
2023-08-05 10:52:16 +02:00
$Reply = "✅ Battery charging... ($Percent%)"
2023-01-21 19:09:01 +01:00
}
2023-08-05 10:52:16 +02:00
} else { # must be offline
2023-08-05 15:11:42 +02:00
if ($Remaining -eq 0) {
$Reply = "✅ Battery at $Percent%, calculating remaining time..."
} elseif ($Remaining -le 5) {
2023-08-05 11:59:15 +02:00
$Reply = "⚠️ Battery at $Percent%, ONLY $Remaining MIN remaining"
} elseif ($Remaining -le 30) {
$Reply = "⚠️ Battery at $Percent%, only $Remaining min remaining"
2023-08-05 12:10:16 +02:00
} elseif ($Percent -lt 10) {
$Reply = "⚠️ Battery $Percent% low, $Remaining min remaining"
2023-08-05 11:59:15 +02:00
} elseif ($Percent -ge 80) {
2023-08-05 10:52:16 +02:00
$Reply = "✅ Battery $Percent% full, $Remaining min remaining"
2023-01-21 19:09:01 +01:00
} else {
2023-08-05 11:59:15 +02:00
$Reply = "✅ Battery at $Percent%, $Remaining min remaining"
2023-01-21 19:09:01 +01:00
}
}
2022-11-30 08:42:00 +01:00
}
2023-08-05 10:52:16 +02:00
Write-Output $Reply
2022-11-30 08:42:00 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2023-08-06 21:35:36 +02:00
}