Windows 8 and 8.1 Forums

Trigger a batch script when power cable is disconnected?

  1. #1

    Posts : 740
    Windows 8.1

    Trigger a batch script when power cable is disconnected?

    Long story short, my Asus has a bug where if I unplug it when the screen is closed the touchscreen's driver is disabled. requiring me to manually reenable it. Everything that you would logically think would stop this bug does not work, not even explicitly telling Windows to not disable USB devices EVER. I would like the problem to go away or resolve itself automatically, but there seems to be no solution to this and Microsoft has Clippy doing technical support on their forums. I can create a script or batch file, but I would like it to trigger automatically, without having it running all the time. Seeing that the problem occurs only when the power is unplugged, I would like to try to have the script run shortly after unplugging.

    Is there a way to trigger a script when the power cable is unplugged, ideally in a clean and efficient manner? thanks.

      My System SpecsSystem Spec

  2. #2

    Posts : 2,072
    Windows 8.0 x64

    I got this from AutoIt3 forums

    You will want to tweak it a bit so it only fires when you haven't run the batch file yet etc..

    As it is it will sit in the tray and check if the Laptop is on AC power every 5 seconds. You specify the batch file(e.g. mybatch.cmd) on the command line

    AutoIt3 is a free programming language for Windows. I recommend getting the package with Scite4AutoIt3. You can compile the script to exe, add custom icons etc..

    Edit: also you will want to get rid of the Msgbox saying the Laptop is plugged in. It's just for test. Otherwise all you will be doing is clicking OK forever.

    Edit2: to exit the infinite loop right click on the tray icon and quit the program.

    #Region Example
    #include <array.au3>
    $batch = ""
    If $CmdLine[0] = 1 Then
        $batch = $CmdLine[1]
    While 1
        $batt = _BatteryQueryEx()
        If $batt[0] = 0 Then
            ;MsgBox(0, "", "The laptop is NOT plugged in")
            If $batch Then
        ElseIf $batt[0] = 1 Then
            MsgBox(0, "", "The laptop IS plugged in")
        Sleep(5000) ; check every 5 seconds
    ;    _BatteryQueryEx()
    ;      original author PsaltyDS
    ;    Rajesh V R - addition of BatteryFullLifeTime
    ;    Return information on the Battery
    ;    Sets @Error on error
    ;    Returns an array:
    ;        $array[0]    = ACPower(0=offline, 1=online, 255=unknown)
    ;        $array[1]    = BatteryFlag(1=High>66%, 2=Low<33%, 4=Critical<5%,
    ;                      8=Charging 128=No Battery, 255=Unknown
    ;                      Use BitAnd to test, ie BitAnd($array[1],128)
    ;        $array[2]    = BatteryLife %(0-100, 255=unknown)
    ;        $array[3]    = Seconds left of charge, estimate(4294967295=unknown)
    ;          $array[4]       = BatteryLife @ Full Capacity (Seconds of battery life if fully charged -1 if info not available Not applicable for AC Source)
    ;     @Link :  
    Func _BatteryQueryEx()
        Local $SystemPower, $ret, $array[5]
        ; Setup $array and $SystemPower
        $SystemPower = DllStructCreate("ubyte;ubyte;ubyte;ubyte;ulong;ulong")
        If @error Then
            Return $array
        ; make the DllCall
        $ret = DllCall("kernel32.dll", "int", "GetSystemPowerStatus", "ptr", DllStructGetPtr($SystemPower))
        If @error Then;DllCall Failed
            $SystemPower = 0
            Return $array
        If Not $ret[0] Then; GetSystemPowerStatus Failed
            $SystemPower = 0
            Return $array
        ; Fill the array
        $array[0] = DllStructGetData($SystemPower, 1);    AC
        $array[1] = DllStructGetData($SystemPower, 2);    Battery Charge
        $array[2] = DllStructGetData($SystemPower, 3);    Battery Charge %
        $array[3] = DllStructGetData($SystemPower, 5);    Sec Battery Left
        $array[4] = DllStructGetData($SystemPower, 6);    Sec Battery Capacity @ Full Charge
        ; free the struct
        $SystemPower = 0
        Return $array
    EndFunc   ;==>_BatteryQueryEx
      My System SpecsSystem Spec

Trigger a batch script when power cable is disconnected?

Similar Threads
Thread Forum
Ethernet cable shows as disconnected
Hi, Sometimes when I start my PC, or I start it from sleep, the Ethernet shows as unplugged. If I go into device manager, it shows a yellow triangle with an exclamation mark in it (attached image). If I 'Scan for hardware changes' it starts to work again, until I shutdown or start from sleep. ...
Network & Sharing
Extension Cable for Power Supply
I want a longer cable for my PC's power supply but I'm not sure what kind of extension I should purchase in order to do so. I would rather just purchase the same kind of cable that came with my PSU (only longer of course) oppose to using a extension cord. Can anyone offer any advice as to what kind...
General Support
Windows 8.1 Freeze on shutdown and plug/unplug power cable
Hey guys, I'm new here; I'm having a problem that is bothering me, I recently installed windows 8.1 on my alienware 17x R2, the problem is the laptop never shutdown and still running !, also when I unplug the power cable then plug it back again the windows freeze ! , Note that the battery on my...
General Support
Problem with string comparison in batch script
Greetings I am attempting to write a batch script that uses a program called Mailsend to send an email. The script then examines Mailsend's log output to determine if the email was successfully sent. This is what I have so far: mailsend1.17b14.exe -to <snip personal info> -log temp.txt...
General Support
Random BSOD Widows 8.1 after being disconnected from power
Hey Guys, New Machine with a issue if you could help , much appreciation in advance, turn on after being disconnected from the power plug back in a few minutes later , turn on , BSOD ,, restarts then starts fine and runs no issues , shut down fine restart fine , take away from a power source...
BSOD Crashes and Debugging
Heating with power cable.
I have next configuration : And I observe something. When Power supply it's connected, CPU become very hot, and Ac power it's dis, the temperature drop down . What should I do? It's that a problem. I have 45 celsius in CPU. when it's disconected. And that in browsing some apps open, like...
Performance & Maintenance
Batch script -> Access Denied to C:\Windows\
Running the following batch script: @regedit /s registry.reg @copy rundll32.exe C:\Windows\ @copy shimgvw.dll C:\Windows\ @echo Done. @pause I am left with the following: 10019 This is new to Windows 8. Why is this happening?
General Support

Eight Forums Android App Eight Forums IOS App Follow us on Facebook