只有一些Windows shell命令通过ruby工作?
我试图使用脚本来控制我的电源选项,因为XP不会给你一个直观的方式来改变CPU频率选项。这里是我的脚本至今:只有一些Windows shell命令通过ruby工作?
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
的问题是,改变根本就不会发生。如果我直接在cmd.exe提示符下运行相同的命令,它们将起作用。这很奇怪,但在最初的powercfg查询后没有任何工作。我觉得我错过了一些非常明显的东西。
我怎样才能得到上面的脚本正常运行?
更新: C:\鞋>红宝石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
尝试
%x[cmd.exe /C ....]
是没有的'标签。这对我来说很有用,当我必须在shell中使用ruby进行操作时。
没有改进。 shell命令似乎在运行,但它们只是停留在自己的世界中。 – Sanarothe 2010-04-12 19:43:55
脚本正常工作(实际上,你并不需要的cmd.exe/C位);如果“输入”是一个整数,而不是像“1”这样的字符串,那么它工作正常。
莫非是你忽略了令人难以置信的明显的事情? 这适用于我(我的系统不支持processor_throttle,所以我测试了其他东西)。
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
然而,这种代码修改的方案(一种轮廓的),而不是在neccesarily活性方案。
我只使用了CMD.exe/C,因为单独的反引号工作不正常,我正在探索其他选项。由于某种原因,当我在命令提示符下执行 POWERCFG/QUERY 1/NUMERICAL时,我可以看到什么看起来是查询的“正确”答案:AC处于常量状态,DC处于自适应状态。当我从ruby脚本中运行相同的命令时,无论我尝试创建哪种shell或脚本更改,我都只能看到Adaptive/Adaptive。我的脚本行更改设置对“真实”查询输出没有影响。 :(我完全迷失了方向。 – Sanarothe 2010-04-12 19:43:13
哦,我重读你说什么。插着to_i到获得,现在我的变化正在发生,但他们仍然只适用于什么Ruby脚本知道是在架构问题,查询到1 /数值仍然从外壳和脚本不同(不同的价值观,相同的名字。) – Sanarothe 2010-04-12 19:46:15
代码加入到回答。 – steenslag 2010-04-12 21:05:04
你需要执行'cmd.exe'(从而创造一个新的shell),每一次? – Syntactic 2010-04-12 12:30:01