mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-16 03:30:39 +01:00
68 lines
1.6 KiB
Markdown
68 lines
1.6 KiB
Markdown
## The *check-battery.ps1* Script
|
|
|
|
This PowerShell script checks and prints the battery status.
|
|
|
|
## Parameters
|
|
```powershell
|
|
check-battery.ps1 [<CommonParameters>]
|
|
|
|
[<CommonParameters>]
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
```
|
|
|
|
## Example
|
|
```powershell
|
|
PS> ./check-battery
|
|
|
|
```
|
|
|
|
## Notes
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
## Related Links
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
## Source Code
|
|
```powershell
|
|
<#
|
|
.SYNOPSIS
|
|
Checks the battery status
|
|
.DESCRIPTION
|
|
This PowerShell script checks and prints the battery status.
|
|
.EXAMPLE
|
|
PS> ./check-battery
|
|
.LINK
|
|
https://github.com/fleschutz/PowerShell
|
|
.NOTES
|
|
Author: Markus Fleschutz | License: CC0
|
|
#>
|
|
|
|
try {
|
|
if ($IsLinux) {
|
|
# TODO
|
|
} else {
|
|
Add-Type -Assembly System.Windows.Forms
|
|
$Details = [System.Windows.Forms.SystemInformation]::PowerStatus
|
|
if ($Details.BatteryChargeStatus -eq "NoSystemBattery") {
|
|
$BatteryStatus = "No battery"
|
|
} else {
|
|
[int]$Percent = 100*$Details.BatteryLifePercent
|
|
[int]$Remaining = $Details.BatteryLifeRemaining / 60
|
|
$BatteryStatus = "Battery $Percent%, $Remaining min left"
|
|
}
|
|
switch ($Details.PowerLineStatus) {
|
|
"Online" { $PowerStatus = "plugged in to AC power" }
|
|
"Offline" { $PowerStatus = "disconnected from AC power" }
|
|
}
|
|
"✅ $BatteryStatus, $PowerStatus"
|
|
}
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|
|
```
|
|
|
|
*Generated by convert-ps2md.ps1 using the comment-based help of check-battery.ps1*
|