PowerShell/Scripts/check-battery.ps1

49 lines
1.3 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
PS> ./check-battery
2023-04-07 19:28:57 +02:00
21% battery life, 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
switch ($Details.PowerLineStatus) {
2023-01-21 19:09:01 +01:00
"Online" {
if ($Details.BatteryChargeStatus -eq "NoSystemBattery") {
$Reply = "✅ AC powered"
} elseif ($Percent -eq 100) {
$Reply = "✅ Battery $Percent% full"
} else {
$Reply = "✅ Battery $Percent%, charging..."
}
}
"Offline" {
if ($Percent -eq 100) {
2023-04-07 19:28:57 +02:00
$Reply = "$Percent% full battery, $Remaining min remaining"
2023-01-21 19:09:01 +01:00
} elseif ($Remaining -gt 30) {
2023-04-07 19:28:57 +02:00
$Reply = "$Percent% battery life, $Remaining min remaining"
2023-01-21 19:09:01 +01:00
} else {
2023-04-07 19:28:57 +02:00
$Reply = "⚠️ $Percent% battery life, only $Remaining min remaining"
2023-01-21 19:09:01 +01:00
}
}
2022-12-01 20:54:52 +01:00
}
2022-11-30 08:42:00 +01:00
}
2023-01-21 19:09:01 +01:00
Write-Host $Reply
2022-11-30 08:42:00 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}