PowerShell/Docs/check-battery.md

90 lines
2.4 KiB
Markdown
Raw Normal View History

2023-07-29 10:34:04 +02:00
*check-battery.ps1*
================
2022-12-04 10:40:18 +01:00
2023-05-26 12:20:18 +02:00
This PowerShell script queries the status of the system battery and prints it.
2022-12-04 10:40:18 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2022-12-04 10:40:18 +01:00
```powershell
2023-07-29 10:15:44 +02:00
PS> ./check-battery.ps1 [<CommonParameters>]
2022-12-04 10:40:18 +01:00
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2022-12-04 10:40:18 +01:00
```powershell
2023-08-06 21:36:33 +02:00
PS> ./check-battery.ps1
2023-08-06 11:42:46 +02:00
⚠️ Battery 9% low, 54 min remaining
2022-12-04 10:40:18 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-12-04 10:40:18 +01:00
Author: Markus Fleschutz | License: CC0
2023-07-29 10:04:38 +02:00
Related Links
-------------
2022-12-04 10:40:18 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-12-04 10:40:18 +01:00
```powershell
<#
.SYNOPSIS
2023-05-26 12:20:18 +02:00
Checks the battery
2022-12-04 10:40:18 +01:00
.DESCRIPTION
2023-05-26 12:20:18 +02:00
This PowerShell script queries the status of the system battery and prints it.
2022-12-04 10:40:18 +01:00
.EXAMPLE
2023-08-06 21:36:33 +02:00
PS> ./check-battery.ps1
2023-08-06 11:42:46 +02:00
⚠️ Battery 9% low, 54 min remaining
2022-12-04 10:40:18 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
try {
if ($IsLinux) {
2023-05-26 12:20:18 +02:00
$Reply = "✅ AC powered" # TODO, just guessing :-)
2022-12-04 10:40:18 +01:00
} else {
Add-Type -Assembly System.Windows.Forms
$Details = [System.Windows.Forms.SystemInformation]::PowerStatus
2023-05-26 12:20:18 +02:00
[int]$Percent = 100 * $Details.BatteryLifePercent
[int]$Remaining = $Details.BatteryLifeRemaining / 60
2023-08-06 11:42:46 +02:00
if ($Details.PowerLineStatus -eq "Online") {
2023-05-26 12:20:18 +02:00
if ($Details.BatteryChargeStatus -eq "NoSystemBattery") {
$Reply = "✅ AC powered"
2023-08-06 11:42:46 +02:00
} elseif ($Percent -ge 95) {
$Reply = "✅ Battery fully charged ($Percent%)"
2023-05-26 12:20:18 +02:00
} else {
2023-08-06 11:42:46 +02:00
$Reply = "✅ Battery charging... ($Percent%)"
2023-05-26 12:20:18 +02:00
}
2023-08-06 11:42:46 +02:00
} else { # must be offline
if ($Remaining -eq 0) {
$Reply = "✅ Battery at $Percent%, calculating remaining time..."
} elseif ($Remaining -le 5) {
$Reply = "⚠️ Battery at $Percent%, ONLY $Remaining MIN remaining"
} elseif ($Remaining -le 30) {
$Reply = "⚠️ Battery at $Percent%, only $Remaining min remaining"
} elseif ($Percent -lt 10) {
$Reply = "⚠️ Battery $Percent% low, $Remaining min remaining"
} elseif ($Percent -ge 80) {
$Reply = "✅ Battery $Percent% full, $Remaining min remaining"
2023-05-26 12:20:18 +02:00
} else {
2023-08-06 11:42:46 +02:00
$Reply = "✅ Battery at $Percent%, $Remaining min remaining"
2023-05-26 12:20:18 +02:00
}
}
2022-12-04 10:40:18 +01:00
}
2023-08-06 11:42:46 +02:00
Write-Output $Reply
2022-12-04 10:40:18 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
```
2023-08-06 21:36:33 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of check-battery.ps1 as of 08/06/2023 21:36:05)*