computing
  • 0

Solved Batch File Request Admin

  • 0

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.

Share

1 Answer

  1. Technically the batch script doesn’t start until after the first window has disappeared. The first window is just getting admin access for the second window which means you can’t have an admin cmd window without the first cmd window. Also, I believe the only way to just have one window pop open and stay open is if you get rid of the top part of your script that asks for admin and just right-hand click on your batch file and click ‘Run as Administrator’.
    • 0