在提升模式下运行 ruby​​ 脚本 [英] Run ruby script in elevated mode

查看:35
本文介绍了在提升模式下运行 ruby​​ 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

这是如何做到的.最简单的方法是使用 ShellExecute 以提升的(管理员)权限重新启动可执行文件.

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

使用 Ruby,您可以这样做:

With Ruby you do it like this:

require 'win32ole'

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

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

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.

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

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

这篇关于在提升模式下运行 ruby​​ 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆