PowerShell/Scripts/check-power.ps1

57 lines
1.8 KiB
PowerShell
Raw Normal View History

2022-11-30 08:42:00 +01:00
<#
.SYNOPSIS
2023-08-23 17:14:06 +02:00
Checks the power status
2022-11-30 08:42:00 +01:00
.DESCRIPTION
2023-08-23 17:14:06 +02:00
This PowerShell script queries the power status and prints it.
2022-11-30 08:42:00 +01:00
.EXAMPLE
2023-08-23 17:14:06 +02:00
PS> ./check-power.ps1
Battery at 9% (54 min remaining) with power scheme: HP Optimized
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) {
2023-08-23 17:14:06 +02:00
$Reply = "✅ Battery at $Percent%"
2023-08-05 15:11:42 +02:00
} elseif ($Remaining -le 5) {
2023-08-24 19:09:32 +02:00
$Reply = "⚠️ Battery at $Percent%, ONLY $Remaining MIN remaining"
2023-08-05 11:59:15 +02:00
} elseif ($Remaining -le 30) {
2023-08-24 19:09:32 +02:00
$Reply = "⚠️ Battery at $Percent%, only $Remaining min remaining"
2023-08-05 12:10:16 +02:00
} elseif ($Percent -lt 10) {
2023-08-24 19:09:32 +02:00
$Reply = "⚠️ Battery at $Percent% with $Remaining min remaining"
2023-08-05 11:59:15 +02:00
} elseif ($Percent -ge 80) {
2023-08-24 19:09:32 +02:00
$Reply = "✅ Battery $Percent% full with $Remaining min remaining"
2023-01-21 19:09:01 +01:00
} else {
2023-08-24 19:09:32 +02:00
$Reply = "✅ Battery at $Percent% with $Remaining min remaining"
2023-01-21 19:09:01 +01:00
}
}
2023-08-23 17:14:06 +02:00
$PowerScheme = (powercfg /getactivescheme)
2023-08-24 19:09:32 +02:00
$PowerScheme = $PowerScheme -Replace "^(.*) \(",""
2023-08-23 17:14:06 +02:00
$PowerScheme = $PowerScheme -Replace "\)$",""
2023-08-24 19:09:32 +02:00
$Reply += ", power scheme is '$PowerScheme'"
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
}