简体   繁体   English

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

[英]Run ruby script in elevated mode

I need to run a ruby script in elevated mode (Admin priviledges) under Windows.我需要在 Windows 下以提升模式(管理员权限)运行 ruby​​ 脚本。 Is it possible?是否可以?

Here's how to do it.这是如何做到的。 The easiest way is to restart your executable with elevaded (Admin) privileges using ShellExecute .最简单的方法是使用ShellExecute以提升的(管理员)权限重新启动可执行文件。

With Ruby you do it like this:使用 Ruby 你可以这样做:

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.如果您启用了 Windows UAC,这将为您提供熟悉的 Windows 弹出对话框,请求管理员权限。 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.这里的秘密技巧是使用未记录的ShellExecute操作参数runas ,它将提升请求的操作。

http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx 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 http://www.sevenforums.com/tutorials/3718-elevated-command-prompt-shortcut.html

I would like to thank Casper and thegreendroid for this modified solution.我要感谢 Casper 和 thegreendroid 提供了这个修改后的解决方案。

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.我对execute_command进行了一些搜索,因为我安装的 ruby​​ 1.9.3 不知道如何处理它,而且我找不到任何东西,所以我使用了反引号。 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 . 2>&1位是如此 ruby​​ 实际获取输出而不是空白字符串,如果该输出与正则表达式/ERROR/匹配,那么您没有管理员权限,因此我们希望它返回nil

This will relaunch itself with administrative privileges then load whatever you put in the require with the comment after it.这将使用管理权限重新启动,然后加载您在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

You could drop the def block and change the if statement to您可以删除def块并将if语句更改为

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

for the sake of brevity.为简洁起见。 Also you could lose the shell variable:您也可能会丢失shell变量:

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

Possible Gotcha: This could infinite loop if running_in_admin_mode?可能的问题:如果running_in_admin_mode?这可能会无限循环running_in_admin_mode? fails repeatedly, but it worked perfectly for me.反复失败,但它对我来说非常有效。

Thanks to other authors, I've come to work with this (tested on windows 8):感谢其他作者,我开始处理这个问题(在 Windows 8 上测试过):

Add this at the top of a ruby script:在 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()

Or you could just have a launcher.cmd containing或者你可以只拥有一个包含

cd full\path
ruby myscript.rb

and launch this cmd file with admin rights并以管理员权限启动此 cmd 文件

Once you've tested with ruby you can try rubyw一旦你用 ruby​​ 测试过,你就可以试试 ruby​​w

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归功于 Peter McEvoy 在此处的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM