如何以管理员身份在 rake 任务中运行 shell 命令? [英] How do I run a shell command inside a rake task as administrator?

查看:45
本文介绍了如何以管理员身份在 rake 任务中运行 shell 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短的 .cmd 文件,我想在部署过程中运行它.不幸的是 .cmd 文件需要管理员权限.是否可以从 rake 中获得管理员权限,或者我是否需要以管理员身份启动 shell?

I have a short .cmd file which I would like to run as part of my deployment process. Unfortunately the .cmd file requires administrator privileges. Is it possible to get administrator permission from within rake, or do I need to start the shell as admin?

推荐答案

你可以试试runas 命令.我不知道你的 rake 任务是什么样的,但如果你正在运行 Kernel#system,试试

task :foo do 
  system "runas /profile /user:#{ENV["COMPUTERNAME"]}/Administrator mybatchfile.cmd"
end

唯一的问题是,runas 会在 shell 中提示输入凭据.所以,你必须是互动的.

Only trouble is, runas prompts for credentials right there in the shell. So, you'd have to be interactive.

irb > 系统runas ..."
输入 FOOBAR/管理员的密码:

irb > system "runas ..."
Enter the password for FOOBAR/Administrator:

有关于 SO 的另一个问题的这个看起来很讨厌的批处理/WSH 答案.我不知道你把命令放在哪里,这看起来也是交互式的.

There's this nasty looking batch/WSH answer from another question on SO. I don't know where you put your command, this looks interactive, as well.

您可以尝试支持显示 UAC 提示的 PowerShell Start-Process cmdlet.

You might try the PowerShell Start-Process cmdlet that supports showing a UAC prompt.

PS> Start-Process mybatchfile.cmd -Verb runas

或者,在 Rake 中

Or, in Rake

task :foo do
  system "powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -command \"Start-Process mybatchfile.cmd -Verb runas\""
end

但这也会启动一个 UAC 对话框.整个过程将需要是交互式的.您不能拥有交互式构建脚本.您唯一的选择是允许您的构建服务器在关闭 UAC 的情况下运行...然后,您无需执行任何操作,因为默认情况下您的所有提示都将是管理员".

But that will also launch a UAC dialog. The whole process is going to need to be interactive. You can't have an interactive build script. Your only choice is allowing your build server to run with UAC off... then, you don't have to do anything, because all your prompts will be Admin by default.

这篇关于如何以管理员身份在 rake 任务中运行 shell 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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