2024-01-25 13:37:12 +01:00
|
|
|
Script: *check-power.ps1*
|
|
|
|
========================
|
2023-09-01 17:53:03 +02:00
|
|
|
|
|
|
|
This PowerShell script queries the power status and prints it.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
```powershell
|
|
|
|
PS> ./check-power.ps1 [<CommonParameters>]
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
```powershell
|
|
|
|
PS> ./check-power.ps1
|
2024-08-15 09:51:46 +02:00
|
|
|
⚠️ Battery at 9% with 54min remaining · power scheme 'HP Optimized'
|
2023-09-01 17:53:03 +02:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
|
|
|
|
Related Links
|
|
|
|
-------------
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
|
|
|
Script Content
|
|
|
|
--------------
|
|
|
|
```powershell
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Checks the power status
|
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script queries the power status and prints it.
|
|
|
|
.EXAMPLE
|
|
|
|
PS> ./check-power.ps1
|
2024-08-15 09:51:46 +02:00
|
|
|
⚠️ Battery at 9% with 54min remaining · power scheme 'HP Optimized'
|
2023-09-01 17:53:03 +02:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ($IsLinux) {
|
2023-10-19 08:12:00 +02:00
|
|
|
$reply = "✅ AC powered" # TODO, just guessing :-)
|
2023-09-01 17:53:03 +02:00
|
|
|
} else {
|
|
|
|
Add-Type -Assembly System.Windows.Forms
|
2023-10-19 08:12:00 +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-03-27 17:36:59 +01:00
|
|
|
$reply = "✅ Battery $percent% full"
|
2023-09-01 17:53:03 +02:00
|
|
|
} else {
|
2024-01-03 12:11:22 +01:00
|
|
|
$reply = "✅ Battery charging ($percent%)"
|
2023-09-01 17:53:03 +02:00
|
|
|
}
|
|
|
|
} else { # must be offline
|
2023-10-19 08:12:00 +02:00
|
|
|
if (($remaining -eq 0) -and ($percent -ge 60)) {
|
|
|
|
$reply = "✅ Battery $percent% full"
|
|
|
|
} elseif ($remaining -eq 0) {
|
|
|
|
$reply = "✅ Battery at $percent%"
|
|
|
|
} elseif ($remaining -le 5) {
|
2024-08-15 09:51:46 +02:00
|
|
|
$reply = "⚠️ Battery at $percent% with ONLY $($remaining)min remaining"
|
2023-10-19 08:12:00 +02:00
|
|
|
} elseif ($remaining -le 30) {
|
2024-08-15 09:51:46 +02:00
|
|
|
$reply = "⚠️ Battery at $percent% with only $($remaining)min remaining"
|
2023-10-19 08:12:00 +02:00
|
|
|
} elseif ($percent -lt 10) {
|
2024-08-15 09:51:46 +02:00
|
|
|
$reply = "⚠️ Battery at $percent% with $($remaining)min remaining"
|
|
|
|
} elseif ($percent -ge 80) {
|
|
|
|
$reply = "✅ Battery $percent% full with $($remaining)min remaining"
|
2023-09-01 17:53:03 +02:00
|
|
|
} else {
|
2024-08-15 09:51:46 +02:00
|
|
|
$reply = "✅ Battery at $percent% with $($remaining)min remaining"
|
2023-09-01 17:53:03 +02:00
|
|
|
}
|
|
|
|
}
|
2023-10-19 08:12:00 +02:00
|
|
|
$powerScheme = (powercfg /getactivescheme)
|
|
|
|
$powerScheme = $powerScheme -Replace "^(.*) \(",""
|
|
|
|
$powerScheme = $powerScheme -Replace "\)$",""
|
2024-08-15 09:51:46 +02:00
|
|
|
$reply += ", power scheme is '$powerScheme'"
|
2023-09-01 17:53:03 +02:00
|
|
|
}
|
2024-08-15 09:51:46 +02:00
|
|
|
Write-Host $reply
|
2023-09-01 17:53:03 +02:00
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-08-15 09:51:46 +02:00
|
|
|
*(generated by convert-ps2md.ps1 using the comment-based help of check-power.ps1 as of 08/15/2024 09:50:46)*
|