Search code examples
windowsbatch-filecmdtablet

windows batch file checking power plug with Battery.PowerOnline returns always FALSE


I did on a lot of windows 10 tablets a shutdown check when the power in the room switched off. Now I tried to update a view dell 8.1 tablets, but the Battery.PowerOnline returns always false. Any ideas?

FOR /F "tokens=* delims=" %%A IN ('WMIC /NameSpace:"\\root\WMI" Path BatteryStatus              Get PowerOnline^,RemainingCapacity /Format:list ^| FIND "="') DO SET Battery.%%A
FOR /F "tokens=* delims=" %%A IN ('WMIC /NameSpace:"\\root\WMI" Path BatteryRuntime             Get EstimatedRuntime               /Format:list ^| FIND "="') DO SET Battery.%%A
FOR /F "tokens=* delims=" %%A IN ('WMIC /NameSpace:"\\root\WMI" Path BatteryFullChargedCapacity Get FullChargedCapacity            /Format:list ^| FIND "="') DO SET Battery.%%A

IF /I "%Battery.PowerOnline%"=="TRUE" (
    SET doShutDown=0
) ELSE (
    SET doShutDown=1
)

Solution

  • You set Battery.%%A which will not set anything to be honest, also, you never defined %Battery.PowerOnline% variable, so it will never return true. Do we really need to check each status? Only the last set from your three commands will keep the variable value, so rather check the main poweronline value and do something from there. Notice how I set the variables:

    @echo off
    for /F "tokens=* delims=" %%A IN ('WMIC /NameSpace:"\\root\WMI" Path BatteryStatus Get PowerOnline /Format:list ^| FIND "PowerOnline"') DO SET "Battery=%%A" & goto :verify
    
    :verify
    if /I "%Battery%"=="PowerOnline=TRUE" (
        set doShutDown=0
    ) else (
        set doShutDown=1
    )