2023-10-31 12:05:32 +01:00
|
|
|
|
<#
|
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
|
2024-07-22 19:56:54 +02:00
|
|
|
|
⚠️ Battery at 9% with 54min remaining · 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 {
|
2022-11-30 16:45:11 +01:00
|
|
|
|
if ($IsLinux) {
|
2023-09-30 17:59:31 +02:00
|
|
|
|
$reply = "✅ AC powered" # TODO, just guessing :-)
|
2022-11-30 16:45:11 +01:00
|
|
|
|
} else {
|
|
|
|
|
Add-Type -Assembly System.Windows.Forms
|
2023-09-30 17:59:31 +02:00
|
|
|
|
$details = [System.Windows.Forms.SystemInformation]::PowerStatus
|
|
|
|
|
[int]$percent = 100 * $details.BatteryLifePercent
|
|
|
|
|
[int]$remaining = $details.BatteryLifeRemaining / 60
|
|
|
|
|
if ($details.PowerLineStatus -eq "Online") {
|
|
|
|
|
if ($details.BatteryChargeStatus -eq "NoSystemBattery") {
|
|
|
|
|
$reply = "✅ AC powered"
|
|
|
|
|
} elseif ($percent -ge 95) {
|
2024-01-27 10:58:07 +01:00
|
|
|
|
$reply = "✅ Battery $percent% full"
|
2023-01-21 19:09:01 +01:00
|
|
|
|
} else {
|
2024-01-02 14:38:53 +01: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-10-17 21:17:04 +02:00
|
|
|
|
if (($remaining -eq 0) -and ($percent -ge 60)) {
|
2023-09-30 17:59:31 +02:00
|
|
|
|
$reply = "✅ Battery $percent% full"
|
|
|
|
|
} elseif ($remaining -eq 0) {
|
|
|
|
|
$reply = "✅ Battery at $percent%"
|
|
|
|
|
} elseif ($remaining -le 5) {
|
2024-07-22 19:56:54 +02:00
|
|
|
|
$reply = "⚠️ Battery at $percent% with ONLY $($remaining)min remaining"
|
2023-09-30 17:59:31 +02:00
|
|
|
|
} elseif ($remaining -le 30) {
|
2024-07-22 19:56:54 +02:00
|
|
|
|
$reply = "⚠️ Battery at $percent% with only $($remaining)min remaining"
|
2023-09-30 17:59:31 +02:00
|
|
|
|
} elseif ($percent -lt 10) {
|
2024-07-22 19:56:54 +02:00
|
|
|
|
$reply = "⚠️ Battery at $percent% with $($remaining)min remaining"
|
|
|
|
|
} elseif ($percent -ge 80) {
|
|
|
|
|
$reply = "✅ Battery $percent% full with $($remaining)min remaining"
|
2023-01-21 19:09:01 +01:00
|
|
|
|
} else {
|
2024-07-22 19:56:54 +02:00
|
|
|
|
$reply = "✅ Battery at $percent% with $($remaining)min remaining"
|
2023-01-21 19:09:01 +01:00
|
|
|
|
}
|
2022-12-19 19:33:32 +01:00
|
|
|
|
}
|
2023-09-30 17:59:31 +02:00
|
|
|
|
$powerScheme = (powercfg /getactivescheme)
|
|
|
|
|
$powerScheme = $powerScheme -Replace "^(.*) \(",""
|
|
|
|
|
$powerScheme = $powerScheme -Replace "\)$",""
|
2024-07-22 19:56:54 +02:00
|
|
|
|
$reply += ", power scheme is '$powerScheme'"
|
2022-11-30 08:42:00 +01:00
|
|
|
|
}
|
2024-05-26 12:31:05 +02: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
|
2023-08-06 21:35:36 +02:00
|
|
|
|
}
|