2022-11-30 08:42:00 +01:00
|
|
|
|
<#
|
|
|
|
|
.SYNOPSIS
|
|
|
|
|
Checks the battery status
|
|
|
|
|
.DESCRIPTION
|
|
|
|
|
This PowerShell script checks and prints the battery status.
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
PS> ./check-battery
|
|
|
|
|
.LINK
|
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
.NOTES
|
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
|
#>
|
|
|
|
|
|
|
|
|
|
try {
|
2022-11-30 16:45:11 +01:00
|
|
|
|
if ($IsLinux) {
|
|
|
|
|
# TODO
|
|
|
|
|
} else {
|
|
|
|
|
Add-Type -Assembly System.Windows.Forms
|
|
|
|
|
$Details = [System.Windows.Forms.SystemInformation]::PowerStatus
|
2022-12-19 19:33:32 +01:00
|
|
|
|
$Status = "✅"
|
|
|
|
|
switch ($Details.PowerLineStatus) {
|
|
|
|
|
"Online" { $Power = "AC powered" }
|
|
|
|
|
"Offline" { $Power = "No AC power" }
|
|
|
|
|
}
|
2022-12-04 09:42:15 +01:00
|
|
|
|
if ($Details.BatteryChargeStatus -eq "NoSystemBattery") {
|
2022-12-19 19:33:32 +01:00
|
|
|
|
$Battery = "no system battery"
|
2022-12-04 09:42:15 +01:00
|
|
|
|
} else {
|
2022-12-19 19:33:32 +01:00
|
|
|
|
[int]$Percent = 100 * $Details.BatteryLifePercent
|
2022-12-04 09:42:15 +01:00
|
|
|
|
[int]$Remaining = $Details.BatteryLifeRemaining / 60
|
2022-12-19 19:33:32 +01:00
|
|
|
|
if ($Remaining -lt 30) { $Status = "⚠️" }
|
|
|
|
|
$Battery = "$Percent% battery life, $Remaining min. left"
|
2022-12-01 20:54:52 +01:00
|
|
|
|
}
|
2022-12-19 19:33:32 +01:00
|
|
|
|
"$Status $Power, $Battery"
|
2022-11-30 08:42:00 +01:00
|
|
|
|
}
|
|
|
|
|
exit 0 # success
|
|
|
|
|
} catch {
|
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|