How to run a batch file with administrator priveleges with automatic elevation

Do you have build scripts that require you run commands elevated. Do you forget to click run as admin then you get errors halfway through a 20 minute build?

Well, I did all the damn time. I found this script change and it works awesome so I figured I would share with the class.


if _%1_==_payload_ goto :payload

:getadmin
echo %~nx0: elevating self
set vbs=%temp%\getadmin.vbs
echo Set UAC = CreateObject^(“Shell.Application”^) >> “%vbs%”
echo UAC.ShellExecute “%~s0”, “payload %~sdp0 %*”, “”, “runas”, 1 >> “%vbs%”
“%temp%\getadmin.vbs”
del “%temp%\getadmin.vbs”
goto :eof

:payload
echo %~nx0: running payload with parameters:
echo %*
echo —————————————————
cd /d %2
shift
shift
rem put your code here
rem e.g.: perl myscript.pl %1 %2 %3 %4 %5 %6 %7 %8 %9
rem SEEKFORD – added code for build. I left in above for guys that need params in this blog post.
@cd /d “%~dp0”
set msbuilddir=C:\program Files (x86)\MSBuild\12.0\Bin
“%msbuilddir%\msbuild” “%CD%\SSI.Build” /t:Build /p:Configuration=Debug;ReferencePath=%CD%\..\bin
goto :eof

 

This is a modified version that I got from here (thanks to Alois Mahdal) that adds in how to build using msbuild.

Happy Coding! or emmm.. Happy Building!!

Leave a Reply

Your email address will not be published. Required fields are marked *