开发者

Check Battery Status in a Batch File?

I'm trying to find a way to check the battery status on a batch file, i.e stop the execution of the script if the laptop is running on battery. I'm trying with the poercfg command with no results.

All I need 开发者_JS百科is Something like that but in a batch file:

#!/bin/bash
if [ acpi -a | grep "off-line" eq 0 ]; 
   then echo "plug your laptop and run it again"
   exit 1
fi

What can I use?


If you are on Linux you can get this information from /proc:

#!/bin/bash
if grep -q discharging /proc/acpi/battery/BAT0/state; then
   echo "plug your laptop and run it again"
   exit 1
fi


Something like Rob Vanderwoude's script might work. As expected it's more work. It looks like it's using some WMI information.


you can using $? to get the last return status of grep. I think this will work for you.

#!/bin/bash
acpi -a | grep "off-line"
if [ $? -eq 0 ]; 
   then echo "plug your laptop and run it again"
   exit 1
fi


Here it goes:

@echo off
wmic Path Win32_Battery Get BatteryStatus | find "2" > nul
if %errorlevel% neq 0 (
  echo Not running on AC, exiting
  exit /B
)
echo running on AC, continuing execution

I have stolen wmic query from Rob's script referenced by Caley


$designCap = Get-WmiObject -Class "BatteryStaticData" -Namespace "ROOT\WMI" |
Group-Object -Property InstanceName -AsHashTable -AsString

Get-CimInstance -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI" |
Select-Object -Property InstanceName, FullChargedCapacity, DesignedCapacity, Percent |
ForEach-Object {
$_.DesignedCapacity = $designCap[$_.InstanceName].DesignedCapacity
$_.Percent = [Math]::Round( ( $_.FullChargedCapacity*100/$_.DesignedCapacity),2)
$_
}

Output

   InstanceName     FullChargedCapacity DesignedCapacity Percent
    ------------     ------------------- ---------------- -------
    ACPI\PNP0C0A\1_0               78455            95008   82,58
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜