topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 2:34 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: [REQ] Disable sleep/hibernation one time to drain battery  (Read 13002 times)

vevola

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 104
  • VeVoLa
    • View Profile
    • Donate to Member
[REQ] Disable sleep/hibernation one time to drain battery
« on: January 05, 2012, 06:31 PM »
rather than having to manually change the settings, is there a little app that will temporarily disable sleep/hibernation so as to drain all the battery?

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #1 on: January 05, 2012, 10:02 PM »
Attached is a small program called PowerMaxTemp, (or PMT for short), that will change the current power scheme to either Always On on XP, (and possibly 2003), or High performance for Vista/7.

Normally both these schemes don't allow Standby/Hibernation, however if you've modified them then you'll just have to change them back now :)
(HINT: If you have changed them, make a copy and call them something else.)

Before changing the power scheme it will determine the current power scheme and then write a command into the RunOnce registry keys, [HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce], to change it back to the current scheme on the next (re)boot.

If you go into the registry editor after running it and look at the entry above you'll see a key similar to the following:

XP:          PMT           powercfg.exe /setactive "Minimal Power Management"
Vista/7:   PMT           powercfg.exe /setactive 381b4222-f694-41f0-9685-ff5bb260df2e

You can delete this key if you don't want it to execute on next boot.

The program will run from CLI or Desktop, there's a bit of error checking to see if powercfg.exe ran OK, it doesn't have any output, (unless there's an error), which I might add later.

It has been tested on my Win7HPx64 Desktop and XP Pro netbook and amazingly it worked.

Have fun.

Source code in AutoIt
Code: AutoIt [Select]
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_Icon=klaptopdaemon.ico
  3. #AutoIt3Wrapper_UseUpx=n
  4. #AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
  5. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  6. ;PowerMaxTemp
  7.  
  8. #include <Array.au3>
  9. #include <Constants.au3>
  10. #include <GUIConstantsEx.au3>
  11. #include <WindowsConstants.au3>
  12. #include <StaticConstants.au3>
  13. #include "_Startup.au3"
  14. #include "_OsVersionInfo.au3"
  15.  
  16. Global $schemes, $MaxPowSch, $CurrPowSch, $Progress
  17.  
  18. If _OsVersionTest($VER_GREATER_EQUAL, 6, 0) Then
  19.         $Progress = 'Vista/7 ' & StringLower(@CPUArch) & ' or higher'
  20.         $Progress = 'XP/2003 ' & StringLower(@CPUArch)
  21. $Progress = $Progress & @CRLF
  22.  
  23. _RevertOldSch()
  24. _GetAllScheme()
  25. _GetMaxPower()
  26. _GetCurrentScheme()
  27. _ResetPowerScheme()
  28. _SetPowerScheme()
  29. _Exit()
  30.  
  31.  
  32. ; Get me outta here!
  33. Func _Exit()
  34.         MsgBox(64, 'PowerMaxTemp', $Progress)
  35.         Exit
  36.  
  37.  
  38. ; Get all power schemes using powercfg.exe /list
  39. Func _GetAllScheme()
  40.         Local $error
  41.         Local $foo = Run(@comspec & ' /c powercfg.exe /list"', '.', @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
  42.         If @error Then
  43.                 MsgBox(48, 'PowerMaxTemp error', 'Failed to run powercfg.exe /list')
  44.         EndIf
  45.         $PowSchList = ''
  46.         While 1
  47.                 $PowSchList &= StdoutRead($foo)
  48.             If @error <> 0 Then ExitLoop
  49.         WEnd
  50.         $error = ''
  51.         While 1
  52.                 $error &= StderrRead($foo)
  53.             If @error <> 0 Then ExitLoop
  54.                 If @extended > 0 Then
  55.                         $Progress = $Progress & 'Power schemes not read' & ' - Code: ' & @error & @CRLF & 'Exiting...'
  56.                         _Exit()
  57.                 EndIf
  58.         WEnd
  59.         StdioClose($foo)
  60.         $schemes = StringSplit(StringStripWS($PowSchList, 4), @CR)
  61.  
  62.  
  63. ; Get the Maximum power scheme:
  64. ; XP: Always On
  65. ; Vista/7: High performance
  66. Func _GetMaxPower()
  67.         Local $i
  68.         For $i = 1 To $schemes[0]
  69.                 If _OsVersionTest($VER_GREATER_EQUAL, 6, 0) Then
  70.                         If StringInStr($schemes[$i], 'High performance') > 0 Then $MaxPowSch = StringMid($schemes[$i], 20, (StringInStr($schemes[$i], ' ', 0, 1, 20) - 20))
  71.                 Else
  72.                         If StringInStr($schemes[$i], 'Always On') > 0 Then $MaxPowSch = "Always On"
  73.                 EndIf
  74.         Next
  75.         $Progress = $Progress & 'New scheme:            ' & $MaxPowSch & @CRLF
  76.  
  77.  
  78. ; Get the current power scheme, in Vista/7 it's marked in the list with '*'.
  79. ; In XP we have to issue a powercfg /query command
  80. ; Vista/7 uses the GUID, XP uses the plain text name  -  Standards, who needs them?
  81. Func _GetCurrentScheme()
  82.         Local $i, $output, $error
  83.         $Progress = $Progress & 'Current scheme:                '
  84.         If _OsVersionTest($VER_GREATER_EQUAL, 6, 0) Then
  85.                 For $i = 1 To $schemes[0]
  86.                         If StringInStr($schemes[$i], '*') > 0 Then $CurrPowSch = StringMid($schemes[$i], 20, (StringInStr($schemes[$i], ' ', 0, 1, 20) - 20))
  87.                 Next
  88.         Else
  89.                 Local $foo = Run(@comspec & ' /c powercfg.exe /query"', '.', @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
  90.                 If @error Then
  91.                         MsgBox(48, 'PowerMaxTemp error', 'Failed to run powercfg.exe /query')
  92.                 EndIf
  93.                 $output = ''
  94.                 While 1
  95.                         $output &= StdoutRead($foo)
  96.                         If @error <> 0 Then ExitLoop
  97.                 WEnd
  98.                 $error = ''
  99.                 While 1
  100.                         $error &= StderrRead($foo)
  101.                         If @error <> 0 Then ExitLoop
  102.                         If @extended > 0 Then
  103.                                 $Progress = $Progress & 'Not read' & ' - Code: ' & @error & @CRLF
  104.                         EndIf
  105.                 WEnd
  106.                 StdioClose($foo)
  107.                 Local $XP = StringSplit(StringStripWS($output, 4), @CR)
  108.                 For $i = 0 To $XP[0]
  109.                         If StringLeft($XP[$i], 4) = 'Name' Then $CurrPowSch = StringStripWS(StringMid($XP[$i], 6), 4)
  110.                 Next
  111.         EndIf
  112.         If @extended <= 0 Then $Progress = $Progress & $CurrPowSch & @CRLF
  113.  
  114.  
  115. ; Set RunOnce to restore the current power scheme on reboot
  116. Func _ResetPowerScheme()
  117.         If _OsVersionTest($VER_GREATER_EQUAL, 6, 0) Then
  118.                 If _StartupRegistry_Install("PMT", "powercfg.exe /setactive " & $CurrPowSch, 1, 1) = 0 Then
  119.                         $Progress = $Progress & 'Install RunOnce:               Fail' & @CRLF
  120.                 Else
  121.                         $Progress = $Progress & 'Install RunOnce:               Done' & @CRLF
  122.                 EndIf
  123.         Else
  124.                 If _StartupRegistry_Install("PMT", 'powercfg.exe /setactive "' & $CurrPowSch & '"', 1, 1) = 0 Then
  125.                         $Progress = $Progress & 'Install RunOnce:               Fail' & @CRLF
  126.                 Else
  127.                         $Progress = $Progress & 'Install RunOnce:               Done' & @CRLF
  128.                 EndIf
  129.         EndIf
  130.  
  131.  
  132. ; Change the system to use the maximum power scheme, for XP we put quotes around it
  133. Func _SetPowerScheme()
  134.         Local $error
  135.         If _OsVersionTest($VER_GREATER_EQUAL, 6, 0) Then
  136.                 Local $foo = Run(@comspec & ' /c powercfg.exe /setactive ' & $MaxPowSch, '.', @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
  137.         Else
  138.                 Local $foo = Run(@comspec & ' /c powercfg.exe /setactive "' & $MaxPowSch & '"', '.', @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
  139.         EndIf
  140.         If @error Then
  141.                 MsgBox(48, 'PowerMaxTemp error', 'Failed to run powercfg.exe /setactive')
  142.         EndIf
  143.         $cps = ''
  144.         While 1
  145.                 $cps &= StdoutRead($foo)
  146.             If @error <> 0 Then ExitLoop
  147.         WEnd
  148.         $error = ''
  149.         While 1
  150.                 $error &= StderrRead($foo)
  151.             If @error <> 0 Then ExitLoop
  152.                 If @extended > 0 Then
  153.                         $Progress = $Progress & 'Set scheme:            Fail' & ' - Code: ' & @error & @CRLF
  154.                 EndIf
  155.         WEnd
  156.         StdioClose($foo)
  157.         If @extended <= 0 Then $Progress = $Progress & 'Set scheme:             Done' & @CRLF
  158.  
  159.  
  160. ; When PMT is run, RunOnce is checked for key 'pmt'.
  161. ; If it exists: the old scheme is read, the command executed, and the 'pmt' key removed.
  162. ; If it doesn't exist the program carries on with setting High performance/Always On scheme.
  163. Func _RevertOldSch()
  164.         If @CPUArch = 'X64' Then
  165.                 Local $OldPowSch = RegRead("HKEY_LOCAL_MACHINE64\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", "pmt")
  166.         Else
  167.                 Local $OldPowSch = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", "pmt")
  168.         EndIf
  169.         If $OldPowSch = '' Then
  170.                 $Progress = $Progress & 'Revert:                        No' & @CRLF
  171.                 Return
  172.         EndIf
  173.         $Progress = $Progress & 'Revert:                        Yes' & @CRLF
  174.         Local $foo = Run(@comspec & ' /c ' & $OldPowSch, '.', @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
  175.         If @error Then
  176.                 MsgBox(48, 'PowerMaxTemp error', 'Failed to run ' & $OldPowSch)
  177.         EndIf
  178.         $cps = ''
  179.         While 1
  180.                 $cps &= StdoutRead($foo)
  181.             If @error <> 0 Then ExitLoop
  182.         WEnd
  183.         $error = ''
  184.         While 1
  185.                 $error &= StderrRead($foo)
  186.             If @error <> 0 Then ExitLoop
  187.                 If @extended > 0 Then
  188.                         $Progress = $Progress & 'Scheme reset:          Fail' & ' - Code: ' & @error & @CRLF
  189.                 EndIf
  190.         WEnd
  191.         StdioClose($foo)
  192.         If @extended <= 0 Then $Progress = $Progress & 'Scheme reset:           Done' & @CRLF
  193.         If _StartupRegistry_Uninstall("PMT", $OldPowSch, 1, 1) = 0 Then
  194.                 $Progress = $Progress & 'Remove RunOnce:                Fail' & @CRLF
  195.         Else
  196.                 $Progress = $Progress & 'Remove RunOnce:                Done' & @CRLF
  197.         EndIf
  198.         _Exit()


Updated:
20120112 - Running it a second time reverts to the original power scheme and clears the RunOnce\pmt key.
20120120 - Outputs results into a window.
« Last Edit: January 19, 2012, 11:28 PM by 4wd »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #2 on: January 12, 2012, 02:00 AM »
Updated, see above - now reverts system on second run.

vevola

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 104
  • VeVoLa
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #3 on: January 12, 2012, 05:02 AM »
Just curious as to whether I should be "seeing" something (some type of visual feedback), because I don't...  :-[

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #4 on: January 12, 2012, 05:15 AM »
Just curious as to whether I should be "seeing" something (some type of visual feedback), because I don't...  :-[

it doesn't have any output, (unless there's an error)

Sounds like all is good ;D

vevola

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 104
  • VeVoLa
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #5 on: January 12, 2012, 05:50 AM »
cool! thanks!!

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #6 on: January 12, 2012, 09:25 PM »
No visual output.....I'm a bit of a lazy coder :D

However, you should get aural feedback.  When my machines are switched to High performance/Always On the CPU fan goes into high gear ;)

I'll look at providing some kind of output window at the end.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #7 on: January 19, 2012, 11:32 PM »
Updated - now outputs to a window.

Run it once:
2012-01-20_15-35-27.jpg

Run it again:
2012-01-20_15-35-07.jpg

It will say Fail instead of Done if the powercfg.exe command failed, (and give the error), or the RunOnce registry entry couldn't be (re)set.

Unfortunately, it's not easy to determine how an AutoIt executable was launched, (CLI or Windows), so the output will always be to a window even if launched from the CLI.

I think that about covers it unless there's a problem - I might look at enabling/disabling Hibernate/Sleep from within the script, (to reduce reliance on powercfg.exe), but don't count on it.

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #8 on: January 20, 2012, 12:38 PM »
Nice work, 4wd.  :Thmbsup: vevola, please confirm this works for you and I'll mark and move this thread to Finished Programs. 

vevola

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 104
  • VeVoLa
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #9 on: February 18, 2012, 09:13 AM »
Well, it seems to first hibernate, and then on wake up it works...

Also... should I be running it *twice*? I'm not sure I understand your earlier post with the two screenshots...

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #10 on: February 19, 2012, 03:20 AM »
Well, it seems to first hibernate, and then on wake up it works...

What OS are you using?
What is the Power Scheme before you run it, (Control Panel->Power Options) ?
What is the Power Scheme after you run it ?

Always On or High Performance does not normally allow Sleep, Standby or Hibernate unless the Power Scheme has been changed by the user, (you should make a copy and change that instead).

Also... should I be running it *twice*? I'm not sure I understand your earlier post with the two screenshots...

Updated:
20120112 - Running it a second time reverts to the original power scheme and clears the RunOnce\pmt key.
20120120 - Outputs results into a window.

That's what the Revert in the output window signifies, Revert=Yes means it's being run for the second time and is setting the Power Scheme to it's original setting prior to the first time PMT was run, it also clears the RunOnce registry entry.

vevola

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 104
  • VeVoLa
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #11 on: March 14, 2012, 03:41 PM »
I'm running Win7 64x with "Balanced" power scheme.

To be honest, I tried using the revert, but now hibernation doesn't work at all. When it goes to sleep, I can't wake it up anymore. I see "resuming", and then a black screen and I have to force shut it down. I then see "delete restoration data" or "resume", and I have to "delete" for windows to boot. Which also means that if I forget to save whatever I have open and go away for a while... well... I loose everything.

Any tips?

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #12 on: March 14, 2012, 07:41 PM »
OK, I'm going to need some more information:

Is your operating system on an SSD or HDD?

The output of the powercfg list command, (see below):

C:\>powercfg list

Existing Power Schemes (* Active)
-----------------------------------
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced) *
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c  (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a  (Power saver)
Power Scheme GUID: e12bd28e-68a8-4f90-b0a5-f76b2b523ca3  (AMD Fusion Utility Recovery Profile)

Can you do a screengrab of PMTs output window when you run it both the first and second time, (like I did here), and either PM them to me or attach to a post.

Can you do a screengrab of both the Balanced and High Performance Advanced settings of your Power Options, the default for both of them should look like this:

Screenshot - 2012_03_15 , 11_33_30.jpg Screenshot - 2012_03_15 , 11_34_59.jpg

Also, after running it once what is the value of the PMT key stored under HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce (use regedit.exe) ?

What is it after running PMT a second time, (it shouldn't be there) ?

vevola

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 104
  • VeVoLa
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #13 on: March 15, 2012, 04:45 AM »
Is your operating system on an SSD or HDD?
SSD

The output of the powercfg list command
Microsoft Windows [Version 6.1.7601]
Existing Power Schemes (* Active)
-----------------------------------
Power Scheme GUID: 381b4222-f694-41f0-9685-ff5bb260df2e  (Balanced)
Power Scheme GUID: 49ef8fc0-bb7f-488e-b6a0-f1fc77ec649b  (Dell)
Power Scheme GUID: 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c  (High performance)
Power Scheme GUID: a1841308-3541-4fab-bc81-f71556f20b4a  (Power saver) *

Can you do a screengrab of PMTs output window when you run it both the first and second time

Can you do a screengrab of both the Balanced and High Performance Advanced settings of your Power Options

Here's an archive with those screengrabs: http://dl.dropbox.co.../pmt_screenshots.rar


Also, after running it once what is the value of the PMT key stored under HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce (use regedit.exe) ?

powercfg.exe /setactive a1841308-3541-4fab-bc81-f71556f20b4a

What is it after running PMT a second time, (it shouldn't be there) ?

And it's not.

Thanks for your help!

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: [REQ] Disable sleep/hibernation one time to drain battery
« Reply #14 on: March 15, 2012, 07:42 AM »
Thanks, it shows that PMT is, (or trying to), switch from Power Saver to High Performance.

You didn't include the High Performance Advanced settings screengrab, could you please?

I'm running Win7 64x with "Balanced" power scheme.

The powercfg output shows you're running in the Power Saver scheme which is a bit more frugal than the Balanced scheme.

It also seems to indicate that there might be some form of power software installed by Dell, (only because I see a Dell power scheme there - AMD do the same thing if you run their Fusion software IIRC).

Thanks for your help!

No problem, it's just got me beat how it can be messing up your hibernation when all it does is swap power schemes, it doesn't change anything in those schemes.  It shouldn't even be going into Sleep/Hibernation when in High Performance, as you can see from my grab above, they're both set to Never by default.

You can also reset the power schemes to their default values by pressing the Restore Plan Defaults button under each schemes Advanced settings.

I've attached a small video of what happens when it runs - you can watch it change the power scheme each time it's run, (.7z because you can't attach .mp4 for some reason).

It's the equivalent of you typing in the following commands in a CLI, just as a matter of interest can you effect the same change from the CLI and does it do the same thing Hibernate-wise ?

powercfg /setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c   (should set to High Performance)
powercfg /setactive a1841308-3541-4fab-bc81-f71556f20b4a    (should set back to Power Saver)

Anyone else care to try it with a Win7 laptop and see what happens?