Sanarothe
Sanarothe

Reputation: 302

Only some windows shell commands work via ruby?

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

Upvotes: 0

Views: 767

Answers (3)

steenslag
steenslag

Reputation: 80065

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.

Upvotes: 1

bta
bta

Reputation: 45057

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).

Upvotes: 0

heldopslippers
heldopslippers

Reputation: 840

try

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

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

Upvotes: 0

Related Questions