Jump to content

Windows 10 Battery saving mode


Recommended Posts

; This is how its be done with VBScript
; --------------------------------------
; strComputer = "."
; Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
; Set colItems = objWMIService.ExecQuery("Select * from Win32_Battery",,48)
; For Each objItem in colItems
;    Wscript.Echo objItem.EstimatedChargeRemaining&"%"
; Next

; Conversion to AutoIT script
; ---------------------------
Local $sLevel = Battery_Level ( )
ConsoleWrite ( "Battery level: " & $sLevel & "%" & @CRLF )

Func Battery_Level ( )
    Local $Obj_Item, $Col_Items, $ilevel = 0

    Local $Obj_WMIService = ObjGet ( 'winmgmts:\\' & @ComputerName & '\root\cimv2' )
    If ( IsObj ( $Obj_WMIService ) ) And ( Not @error ) Then
        $Col_Items = $Obj_WMIService.ExecQuery( 'Select * from Win32_Battery' )

        For $Obj_Item In $Col_Items
            $ilevel = $Obj_Item.EstimatedChargeRemaining
        Next
    Else
        MsgBox ( 48, "Error", "WMI Object/Error :(" )
    EndIf
    Return $ilevel
EndFunc

I don't have a laptop so i can't test this properly. When i run the code it says: "0%". But perhaps this example gives you some ideas to solve the problem.

Edited by pluto41
Link to comment
Share on other sites

Hello.

 

You have to use GetSystemPowerStatus.

Something like this should work. (I have not laptop to check)

 

Global Const $tagSYSTEM_POWER_STATUS = 'byte ACLineStatus;byte BatteryFlag;byte BatteryLifePercent;byte SystemStatusFlag;' & _
        'int BatteryLifeTime;int BatteryFullLifeTime'

;~ ;SystemStatusFlag
;~ ;Note  This flag was introduced in Windows 10. This flag was previously reserved, named Reserved1, and had a value of 0.

Local $BatterySaverStatus = _GetBatterySaverStatus()
MsgBox(0, "Info", "Battery Saver is : " & ($BatterySaverStatus ? "Enabled" : "Disabled"))



Func _GetBatterySaverStatus()
    Local $tSYSTEM_POWER_STATUS = DllStructCreate($tagSYSTEM_POWER_STATUS)
    Local $aRet = DllCall('kernel32.dll', 'bool', 'GetSystemPowerStatus', 'struct*', $tSYSTEM_POWER_STATUS)
    If @error Or Not $aRet[0] Then Return SetError(1, @extended, 0)
    Return DllStructGetData($tSYSTEM_POWER_STATUS, 4)
EndFunc   ;==>_GetBatterySaverStatus

 

Saludos

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...