【问题标题】:Only some windows shell commands work via ruby?只有一些 Windows shell 命令通过 ruby​​ 工作?
【发布时间】:2010-04-12 12:25:07
【问题描述】:

我正在尝试使用脚本来控制我的电源选项,因为 XP 没有为您提供更改 CPU 频率选项的直观方法。到目前为止,这是我的脚本:

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:\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

【问题讨论】:

  • 是否需要每次都执行cmd.exe(从而创建一个新的shell)?

标签: windows ruby command-line


【解决方案1】:

脚本运行良好(确实,您不需要 cmd.exe /C 位);如果“input”是一个整数,而不是像“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 

但是,此代码更改了一个方案(一种配置文件),而不一定是 active 方案。

【讨论】:

  • 我只使用了 CMD.exe /C 因为单独的反引号不能正常工作,我正在探索其他选项。出于某种原因,当我在命令提示符下执行 POWERCFG /QUERY 1 /NUMERICAL 时,我可以看到查询的“正确”答案:AC 处于恒定状态,DC 处于自适应状态。当我从我的 ruby​​ 脚本中运行相同的命令时,我只会看到 Adaptive/Adaptive,而不管我尝试在 shell 或脚本中进行哪些更改。我更改设置的脚本行对“真实”查询输出没有影响。 :(我很迷茫。
  • 哦,我重读了你说的话。将 to_i 粘贴到 get 上,现在我的更改正在发生,但它们仍然只应用于 ruby​​ 脚本知道的有问题的模式。对 1 /NUMERICAL 的查询仍然与 shell 和脚本不同。 (不同的值,相同的名称。)
【解决方案2】:

试试

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

没有''标签。当我必须在 shell 中用 ruby​​ 做事情时,这对我有用。

【讨论】:

  • 没有改善。 shell 命令似乎可以运行,但它们只是停留在自己的世界中。
【解决方案3】:

不要直接从system 调用运行这些命令,而是尝试将POWERCFG 调用写入.bat 文件并通过system 运行该文件。过去我不得不这样做,以使某些 DOS 应用程序以与在 cmd.exe 窗口中手动执行时相同的方式运行。

编辑:基于示例here,我建议您在进行更改后运行POWERCFG /SETACTIVE Portable/Laptop(以确保您的更改生效)。

【讨论】:

  • /SETACTIVE 使方案成为活动方案。不确定这是否是我们想要的。
猜你喜欢
  • 2018-01-13
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-05
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
相关资源
最近更新 更多