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-01-21 19:09:01 +01:00
Battery 21%, 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) {
$Reply = "✅ Battery $Percent% full, $Remaining min. remaining"
} elseif ($Remaining -gt 30) {
$Reply = "✅ Battery $Percent%, $Remaining min. remaining"
} else {
$Reply = "⚠️ Battery $Percent%, only $Remaining min. remaining"
}
}
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
}