在提升模式下运行 ruby​​ 脚本

发布于 2024-12-18 04:03:06 字数 51 浏览 0 评论 0原文

我需要在 Windows 下以提升模式(管理员权限)运行 ruby​​ 脚本。是否可以?

I need to run a ruby script in elevated mode (Admin priviledges) under Windows. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风不识月 2024-12-25 04:03:06

以下是具体操作方法。最简单的方法是使用 ShellExecute 以提升的(管理员)权限重新启动可执行文件。

使用 Ruby,您可以这样做:

require 'win32ole'

shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute('path_to_ruby_program', nil, nil, 'runas')

如果您启用了 Windows UAC,这将为您提供熟悉的 Windows 弹出对话框,请求管理员权限。单击“是”后,您的进程将以管理员权限运行。

这里的秘密技巧是使用未记录的 ShellExecute 操作参数 runas,这将提升请求的操作。

http://msdn.microsoft .com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

还有关于如何手动创建提升命令的相关讨论提示快捷方式(在某些情况下这可能是一个足够好的解决方案):

http://www.sevenforums.com/tutorials/3718-elevated-command-prompt-shortcut.html

Here's how to do it. The easiest way is to restart your executable with elevaded (Admin) privileges using ShellExecute.

With Ruby you do it like this:

require 'win32ole'

shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute('path_to_ruby_program', nil, nil, 'runas')

If you have Windows UAC enabled this will give you the familiar Windows pop up dialog that requests Admin privileges. Once you click Yes, your process will run with Admin rights.

The secret trick here is using the the undocumented ShellExecute operation parameter runas, which will elevate the requested operation.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx

Also related discussion on how to manually create an elevated command prompt shortcut (which might be a good enough solution in some cases):

http://www.sevenforums.com/tutorials/3718-elevated-command-prompt-shortcut.html

旧时浪漫 2024-12-25 04:03:06

我要感谢 Casper 和 thegreendroid 提供的这个修改后的解决方案。

我无法让他们的示例按原样运行,因此我进行了更多研究,将其放在一起。我对 execute_command 做了一些搜索,因为我安装的 ruby​​ 1.9.3 不知道如何处理它,而且我找不到任何东西,所以我使用了反引号。 \ 必须被转义。 2>&1 位是如此,ruby 实际上获取输出而不是空白字符串,如果该输出与正则表达式 /ERROR/ 匹配,那么您就没有管理员权限,因此我们希望它返回nil

这将以管理权限重新启动,然后加载您在 require 中放入的内容以及后面的注释。

require 'win32ole'
def running_in_admin_mode?
    (`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil? 
end

if running_in_admin_mode?
    require './main.rb' # load the actual program here.
else
    path = 'rubyw.exe ' + File.expand_path(__FILE__) # optionally 'ruby.exe '
    shell = WIN32OLE.new('Shell.Application')
    shell.ShellExecute(path, nil, nil, 'runas')
end

您可以删除 def 块并将 if 语句更改为

if (`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil?

为了简洁起见, 。另外,您可能会丢失 shell 变量:

WIN32OLE.new('Shell.Application').ShellExecute(path, nil, nil, 'runas')

可能的陷阱:如果 running_in_admin_mode? 反复失败,这可能会无限循环,但它对我来说非常有效。

I would like to thank Casper and thegreendroid for this modified solution.

I couldn't get their examples to run as is so with a touch more research I put this together. I did a bit of a search for execute_command, as my installation of ruby 1.9.3 didn't know what to do with it, and I couldn't find anything so I used backticks. The \ had to be escaped. The 2>&1 bit is so ruby actually gets the output instead of a blank string, and if that output matches the Regexp /ERROR/ then you don't have admin privileges, so we want it to return nil.

This will relaunch itself with administrative privileges then load whatever you put in the require with the comment after it.

require 'win32ole'
def running_in_admin_mode?
    (`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil? 
end

if running_in_admin_mode?
    require './main.rb' # load the actual program here.
else
    path = 'rubyw.exe ' + File.expand_path(__FILE__) # optionally 'ruby.exe '
    shell = WIN32OLE.new('Shell.Application')
    shell.ShellExecute(path, nil, nil, 'runas')
end

You could drop the def block and change the if statement to

if (`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil?

for the sake of brevity. Also you could lose the shell variable:

WIN32OLE.new('Shell.Application').ShellExecute(path, nil, nil, 'runas')

Possible Gotcha: This could infinite loop if running_in_admin_mode? fails repeatedly, but it worked perfectly for me.

把时间冻结 2024-12-25 04:03:06

感谢其他作者,我已经开始使用这个(在 Windows 8 上测试):

将其添加到 ruby​​ 脚本的顶部:

def running_in_admin_mode?
  (`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil? 
end

unless running_in_admin_mode?
  require 'win32ole'
  shell = WIN32OLE.new('Shell.Application')
  shell.ShellExecute("ruby", File.expand_path(__FILE__), nil, 'runas')
  exit
end

# admin rights ensured
do_something()

或者您可以只拥有一个包含

cd full\path
ruby myscript.rb

并使用管理员权限启动此 cmd 文件的

launcher.cmd一旦您已经用 ruby​​ 测试过了,你可以尝试 ruby​​w

Thanks to other authors, I've come to work with this (tested on windows 8):

Add this at the top of a ruby script:

def running_in_admin_mode?
  (`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil? 
end

unless running_in_admin_mode?
  require 'win32ole'
  shell = WIN32OLE.new('Shell.Application')
  shell.ShellExecute("ruby", File.expand_path(__FILE__), nil, 'runas')
  exit
end

# admin rights ensured
do_something()

Or you could just have a launcher.cmd containing

cd full\path
ruby myscript.rb

and launch this cmd file with admin rights

Once you've tested with ruby you can try rubyw

奢望 2024-12-25 04:03:06

另一种方法是确保您不会在非管理模式下运行脚本。根据我的经验,我发现这个解决方案令人满意。

可以确定脚本是否在管理模式下运行,如下所示 -

def running_in_admin_mode?
  query_admin_mode_cmd = 'reg query "HKU\S-1-5-19"'
  output, exit_status = execute_command(query_admin_mode_cmd)
  exit_status == 0
end

感谢 Peter McEvoy 的回答 此处

Another method is to ensure you do not run your script in non-admin mode. I have found this solution to be satisfactory in my experience.

It can be determined whether a script is running in admin mode like so -

def running_in_admin_mode?
  query_admin_mode_cmd = 'reg query "HKU\S-1-5-19"'
  output, exit_status = execute_command(query_admin_mode_cmd)
  exit_status == 0
end

Credit goes to Peter McEvoy for his answer here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文