Created
September 6, 2023 15:06
-
-
Save manio/95ad95dc5eba708b12d658f2e6f478ea to your computer and use it in GitHub Desktop.
PowerShell script for sending laptop battery level from Windows10 via UDP to some other host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ABOUT: | |
# I needed to have a battery level percentage of my Windows laptop reported to a linux host somehow. | |
# I am then able to monitor this value and automate switching ON/OFF power supply to keep the laptop | |
# battery charged in ranges about 20-80%, totaly automatically | |
# first you need to download this helper script for sending UDP: | |
# https://gist.githubusercontent.com/PeteGoo/21a5ab7636786670e47c/raw/30cc9ca860154429bcef500fa2d0e96811ad0292/Send-UdpDatagram.ps1 | |
# the scripts neeed to be "unblocked" to be able to run from PowerShell using this method: | |
#dir .\battery_level.ps1|block-File | |
#dir .\Send-UdpDatagram.ps1|Unblock-File | |
# I also added this script to be called every minute from Windows task scheduler | |
# I needed to checkbox to run even on battery (but keep in mind that it won't run anyway if the | |
# battery level is below ~20% !) | |
# https://blog.netwrix.com/2018/07/03/how-to-automate-powershell-scripts-with-task-scheduler/ | |
# taskschd.msc | |
. .\Send-UdpDatagram.ps1 | |
# debug info: | |
#Get-WmiObject -Query 'select EstimatedChargeRemaining from Win32_Battery' | Format-List Estimated* | |
#Get-WmiObject -Namespace 'root\wmi' -Query 'select Charging from BatteryStatus' | Format-List Charging* | |
$level = Get-WmiObject -Query 'select EstimatedChargeRemaining from Win32_Battery' | |
$status = Get-WmiObject -Namespace 'root\wmi' -Query 'select Charging from BatteryStatus' | |
#echo $batt.EstimatedChargeRemaining | |
Send-UdpDatagram -EndPoint "192.168.0.10" -Port 772 -Message "$($level.EstimatedChargeRemaining)|$($status.Charging)`n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment