Run Ruby Script in Elevated Mode

Run ruby script in elevated mode

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

How do I create a ruby script that runs script/console and then runs commands using that environment?

I've found that custom rake tasks are an awesome tool for when you have work which requires running code in the rails environment. Check out this railscast http://railscasts.com/episodes/66-custom-rake-tasks

Execute script with Ruby on Rails?

Seems you just want to run shell commands in ruby code, well you can use system or backtick(`)

system 'ls' # will return ls output in *nix
`dir` # will return dir output in windows

How can I execute Ruby scripts and irb from Windows7 Powershell?

Is the directory that Windows Ruby Installer installed to (like C:\Ruby192\bin) on your path? I can't recall if it should add that automatically on install but I've not had any issues running irb or scripts from powershell. You can check if it's on your path by running the following from a powershell session:

$env:path


Related Topics



Leave a reply



Submit