4

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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Tyler
  • 1,440
  • 2
  • 11
  • 11
  • Please also see 'http://stackoverflow.com/questions/9796028/execute-bash-commands-from-a-rakefile/14360488#14360488' on how to run any external commands in rake. – Gizmomogwai Dec 28 '16 at 14:08

1 Answers1

2

You can try the runas command. I don't know what your rake task looks like, but if you're running Kernel#system, try

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

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

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

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.

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

PS> Start-Process mybatchfile.cmd -Verb runas

Or, in Rake

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

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.

Community
  • 1
  • 1
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188