问题
How can I check if computer is plugged into AC power in a batch file in windows 7, like on_ac_power
does in linux?
回答1:
There's a direct batch file way:
WMIC Path Win32_Battery Get BatteryStatus
Using this and some find
/errorlevel
magic, you should be able to turn it into a condition.
回答2:
A quick google1 dragged up
- A powershell solution
- A C++ solution here. I compiled up the example as battery.exe2. I also coded up a modified program that returns 0 (offline), 1 (online) or 255 (unknown) depending on the ACLineStatus field of the SYSTEM_POWER_STATUS Structure. I called it ACLineStatus.exe. You can use this in a batch file, checking the exit code for one of these values.
Here is the - impressive - C source code to the tool :)
#include <windows.h>
int main()
{
SYSTEM_POWER_STATUS status;
GetSystemPowerStatus( &status );
return status.ACLineStatus;
}
Hope that helps
1 http://www.google.com/search?q=windows%20powershell%20battery%20mains%20status
2 note: cross compiled on Linux since I don't have Windows. It worked under wine though, output:
$./battery.exe 255% -> Amount of time remaining is unknown
回答3:
set OnAC=false
set cmd=WMIC /NameSpace:\\root\WMI Path BatteryStatus Get PowerOnline
%cmd% | find /i "true" > nul && set OnAC=true
if %OnAC% == true *Do your thing here*
回答4:
Here is the script I am using in our environnement, works nicely:
wmic path Win32_Battery Get BatteryStatus | find /v "BatteryStatus" | find "1" >nul 2>&1
if "%errorlevel%" == "0" (echo Do whatever you want if on BATTERY) else (echo Do whatever you want if on AC POWER)
Description:
From the wmic command, isolate the number from the output.
Try to find the number "1" in the result. If succesfull, it means the computer is running on battery only. The official terminology is "(1) The battery is discharging."
Else, the computer is plugged in AC power.
回答5:
You can indeed get the battery / ac status via:
wmic path Win32_Battery Get BatteryStatus
But, evaluating the status value is not just a matter of "is 1" vs "is not 1"!
Check out:
https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-battery
For example, when the AC is plugged in, you should normally get "2"
Unknown (2)
The system has access to AC so no battery is being discharged. However, the battery is not necessarily charging.
But, you could get a collection of other values as well.
I'm fairly confident these all mean "on battery" / "not on AC":
Other (1) Low (4) Critical (5)
And these all mean "on AC" / "not on battery":
Unknown (2) Charging (6) Charging and High (7) Charging and Low (8) Charging and Critical (9)
I strongly guess that this also indicates "on AC":
Fully Charged (3)
These seem less certain...
Undefined (10) Partially Charged (11)
I would guess "Undefined (10)" means "on AC" / "no battery". And "Partially Charged (11)" must mean "on battery", but whether or not the "AC is on" seems pretty nebulous for this (last, odd) enumeration.
Not also, that normally when there is no battery on the machine, this message is returned instead:
"No Instance(s) Available."
In summary, for my purposes, I defined there to be 4 main "states" for a battery to be in:
- DISCHARGING
- CHARGING
- FULL
- NOT PRESENT
On Linux, there are direct analogs for these to be queried from the kernel.
In my logic, I deem first "No Instance(s) Available." == NOT PRESENT
, then I used the following evaluations for the status codes:
DISCHARGING
Other (1)
Low (4)
Critical (5)
CHARGING
Unknown (2)
Charging (6)
Charging and High (7)
Charging and Low (8)
Charging and Critical (9)
Partially Charged (11)
FULL
Fully Charged (3)
NOT PRESENT
Undefined (10)
Additionally, I wanted a boolean check for "isBatteryPresent" and "isAcPower". I leaned on my state evaluation and then deemed the following:
isBatteryPresent = state != NOT PRESENT
isAcPower = state != DISCHARGING
来源:https://stackoverflow.com/questions/7355331/check-if-computer-is-plugged-into-ac-power-in-batch-file