I have a batch file that requires administrative privileges to perform most of its task for which it has this bit of code to do that.
>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”
set params = %*:”=””
echo UAC.ShellExecute “%~s0”, “%params%”, “”, “runas”, 1 >> “%temp%\getadmin.vbs”
“%temp%\getadmin.vbs”
exit /B
:gotAdmin
if exist “%temp%\getadmin.vbs” ( del “%temp%\getadmin.vbs” )
pushd “%CD%”
CD /D “%~dp0”
The above code is put in the top of out batch file.
When it runs it request Admin and performs its task.
Only problem is that it opens up another command prompt with out batch in it and closes the one I opened to get it to open the other one.
What we would like is the batch file to open, ask for admin, then continue to run in the same cmd window. If that is possible.
1 Answer