0

I have a .bat file that call a third party application with some command line arguments added. Now I need this application to run as admin. I found an option to create shortcut to .bat file and then set it to run as admin, but then I am not able to pass command line arguments in this way.

I also found another option to do it as a .vbs script but I need to call this .vbs file from Run dialog and Run needs manually adding of extension for .vbs file.

What should I do in such case?

  • 3
    [How can I auto-elevate my batch file, so that it requests from UAC administrator rights if required?](http://stackoverflow.com/a/28467343) - I've linked this particular answer. – wOxxOm Nov 21 '15 at 20:30

1 Answers1

0

Take a look at this code:

@ECHO OFF
:: this tests if the file is running as admin
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (GOTO askAdmin)
GOTO gotAdmin
:askAdmin
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
::from this point you can execute your command as admin

If you put that at the top of your batch-file it gives the user a "execute as admin" prompt and restarts the current batch-file as admin

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35