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
How to Add a New Action to the Existing Controller
Where in the Ruby Language Is %Q, %W, etc., Defined
How Mix in Routes in Sinatra for a Better Structure
How to Get a Stack Trace Object in Ruby
Iconv Deprecation Warning with Ruby 1.9.3
How to Get the Latest Record from Each Group in Activerecord
How to Use Watir::Waiter::Wait_Until to Force Chrome to Wait
How to Backreference in Ruby Regular Expression (Regex) with Gsub When I Use Grouping
Ruby Rails - Select Only Few Columns from the Data Base
Error Installing Nokogiri 1.6.0 on MAC (Libxml2)
Ruby - Dynamically Add Property to Class (At Runtime)
Convert Non-Breaking Spaces to Spaces in Ruby
Custom Ruby Gem in Gemfile on Heroku
How to Use the Rails Helper "Distance_Of_Time_In_Words" in Plain Old Ruby (Non-Rails)
Devise: How to Override Devise Error Messages on Password Change