在提升模式下运行 ruby 脚本
我需要在 Windows 下以提升模式(管理员权限)运行 ruby 脚本。是否可以?
I need to run a ruby script in elevated mode (Admin priviledges) under Windows. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是具体操作方法。最简单的方法是使用
ShellExecute
以提升的(管理员)权限重新启动可执行文件。使用 Ruby,您可以这样做:
如果您启用了 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:
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 parameterrunas
, 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
我要感谢 Casper 和 thegreendroid 提供的这个修改后的解决方案。
我无法让他们的示例按原样运行,因此我进行了更多研究,将其放在一起。我对
execute_command
做了一些搜索,因为我安装的 ruby 1.9.3 不知道如何处理它,而且我找不到任何东西,所以我使用了反引号。\
必须被转义。2>&1
位是如此,ruby 实际上获取输出而不是空白字符串,如果该输出与正则表达式/ERROR/
匹配,那么您就没有管理员权限,因此我们希望它返回nil
。这将以管理权限重新启动,然后加载您在
require
中放入的内容以及后面的注释。您可以删除
def
块并将if
语句更改为为了简洁起见, 。另外,您可能会丢失
shell
变量:可能的陷阱:如果
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. The2>&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 returnnil
.This will relaunch itself with administrative privileges then load whatever you put in the
require
with the comment after it.You could drop the
def
block and change theif
statement tofor the sake of brevity. Also you could lose the
shell
variable:Possible Gotcha: This could infinite loop if
running_in_admin_mode?
fails repeatedly, but it worked perfectly for me.感谢其他作者,我已经开始使用这个(在 Windows 8 上测试):
将其添加到 ruby 脚本的顶部:
或者您可以只拥有一个包含
并使用管理员权限启动此 cmd 文件的
launcher.cmd一旦您已经用 ruby 测试过了,你可以尝试 rubyw
Thanks to other authors, I've come to work with this (tested on windows 8):
Add this at the top of a ruby script:
Or you could just have a launcher.cmd containing
and launch this cmd file with admin rights
Once you've tested with ruby you can try rubyw
另一种方法是确保您不会在非管理模式下运行脚本。根据我的经验,我发现这个解决方案令人满意。
可以确定脚本是否在管理模式下运行,如下所示 -
感谢 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 -
Credit goes to Peter McEvoy for his answer here