Update check-battery.ps1

This commit is contained in:
Markus Fleschutz 2023-01-21 19:09:01 +01:00
parent 2342f24566
commit 784355e13a

View File

@ -1,10 +1,11 @@
<# <#
.SYNOPSIS .SYNOPSIS
Checks the battery status Checks the battery
.DESCRIPTION .DESCRIPTION
This PowerShell script checks and prints the battery status. This PowerShell script queries the status of the system battery and prints it.
.EXAMPLE .EXAMPLE
PS> ./check-battery PS> ./check-battery
Battery 21%, 54 min. remaining
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -13,25 +14,34 @@
try { try {
if ($IsLinux) { if ($IsLinux) {
# TODO $Reply = "✅ AC powered" # TODO, just guessing :-)
} else { } else {
Add-Type -Assembly System.Windows.Forms Add-Type -Assembly System.Windows.Forms
$Details = [System.Windows.Forms.SystemInformation]::PowerStatus $Details = [System.Windows.Forms.SystemInformation]::PowerStatus
$Status = ""
switch ($Details.PowerLineStatus) {
"Online" { $Power = "AC powered" }
"Offline" { $Power = "No AC power" }
}
if ($Details.BatteryChargeStatus -eq "NoSystemBattery") {
$Battery = "no system battery"
} else {
[int]$Percent = 100 * $Details.BatteryLifePercent [int]$Percent = 100 * $Details.BatteryLifePercent
[int]$Remaining = $Details.BatteryLifeRemaining / 60 [int]$Remaining = $Details.BatteryLifeRemaining / 60
if ($Remaining -lt 30) { $Status = "⚠️" } switch ($Details.PowerLineStatus) {
$Battery = "$Percent% battery life, $Remaining min. left" "Online" {
if ($Details.BatteryChargeStatus -eq "NoSystemBattery") {
$Reply = "✅ AC powered"
} elseif ($Percent -eq 100) {
$Reply = "✅ Battery $Percent% full"
} else {
$Reply = "✅ Battery $Percent%, charging..."
} }
Write-Host "$Status $Power, $Battery"
} }
"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"
}
}
}
}
Write-Host $Reply
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"