views:

93

answers:

3

Hi. I'm trying to use a script to control my power options since XP doesn't give you an intuitive way to change CPU Frequency options. Here's my script so far:

meh = `cmd.exe /C POWERCFG.EXE /QUERY Portable/Laptop`
puts ""
puts meh

    case input
        when 1 then system('cmd.exe /C POWERCFG.EXE /CHANGE Portable/Laptop /processor-throttle-ac NONE')
        when 2 then system('cmd.exe /C POWERCFG.EXE /CHANGE Portable/Laptop /processor-throttle-ac ADAPTIVE')
        when 3 then `cmd.exe /C POWERCFG.EXE /CHANGE Portable/Laptop /processor-throttle-ac CONSTANT`
    end

The problem is that the changes simply don't take place. If I run the same commands directly into a cmd.exe prompt, they work. It's very strange, but nothing works after the initial powercfg query. I feel like I'm missing something incredibly obvious.

How can I get the above script to run correctly?

Update: C:\shoe>ruby freq.rb

Field Description          Value
-----------------          -----
Name                       Portable/Laptop
Numerical ID               1
Turn off monitor (AC)      After 15 mins
Turn off monitor (DC)      After 5 mins
Turn off hard disks (AC)   After 30 mins
Turn off hard disks (DC)   After 5 mins
System standby (AC)        After 20 mins
System standby (DC)        After 5 mins
System hibernates (AC)     Not Supported
System hibernates (DC)     Not Supported
Processor Throttle (AC)    ADAPTIVE
Processor Throttle (DC)    ADAPTIVE
Enter a number to switch portable/laptop profile to that mode.
1 - None (HIGHEST FREQUENCY MODE)
2 - Adaptive (SPEEDSTEP)
3 - Constant (LOWEST FREQUENCY MODE)
#SCRIPT IS CANCELED HERE. I've already tested the methods to change powercfg options, and they work, but they only apply to Ruby's instance of powercfg.

C:\shoe>powercfg /QUERY 1 /NUMERICAL


Field Description          Value
-----------------          -----
Name                       Portable/Laptop
Numerical ID               1
Turn off monitor (AC)      After 15 mins
Turn off monitor (DC)      After 5 mins
Turn off hard disks (AC)   After 30 mins
Turn off hard disks (DC)   After 5 mins
System standby (AC)        After 20 mins
System standby (DC)        After 5 mins
System hibernates (AC)     Not Supported
System hibernates (DC)     Not Supported
Processor Throttle (AC)    CONSTANT
Processor Throttle (DC)    ADAPTIVE
A: 

try

%x[cmd.exe /C ....]

Yes without the '' tags. This works for me when i have to do thing in ruby in the shell.

No improvement. The shell commands seem to run, but they're just stuck in their own world.
Sanarothe
+1  A: 

Script works fine (indeed, you don't need the cmd.exe /C bit); that is it works fine if "input" is an integer, not a string like "1".

Could that be the incredible obvious thing you overlooked? This works for me (my system does not support processor_throttle, so I tested with something else).

def status
  `POWERCFG.EXE /QUERY #{@scheme_name} `
end

@scheme_name = "Portable/Laptop"
puts 'Before:'
puts status
 `POWERCFG.EXE /CHANGE #{@scheme_name}  /disk-timeout-ac 15`
puts '_'*50
puts 'After: '
puts status 

However, this code changes a scheme (kind of a profile), not neccesarily the active scheme.

steenslag
I only used the CMD.exe /C because the backticks alone weren't working correctly, and I was exploring other options. For some reason, when I executePOWERCFG /QUERY 1 /NUMERICAL in command prompt I can see what appears to be the "proper" answer to the query: AC is in Constant and DC is in Adaptive. When I run the same commands from inside my ruby script, I only see Adaptive/Adaptive regardless of what in-shell or in-script changes I try to make. My script lines to change the setting have no effect on the "real" query output.:( I'm quite lost.
Sanarothe
Oh, I reread what you said. Stuck a to_i onto the gets and now my changes are taking place, but they still are only applied to what the ruby script knows to be the schema in question. Queries to 1 /NUMERICAL are still different from the shell and the script. (Different values, same name.)
Sanarothe
Code added to answer.
steenslag
A: 

Instead of running these commands directly from a system call, try writing the POWERCFG call in a .bat file and run that file via system. I have had to do that in the past for some DOS applications to run the same way that they would when executed by hand in a cmd.exe window.

Edit: Based off of the example here, I would recommend running POWERCFG /SETACTIVE Portable/Laptop after you make your changes (to make sure your changes take effect).

bta
/SETACTIVE makes the scheme the active scheme. Not sure if that's what's desired.
steenslag