Mostrando entradas con la etiqueta Software. Mostrar todas las entradas
Mostrando entradas con la etiqueta Software. Mostrar todas las entradas

20140811

Request UAC Elevation with BAT files

There is many reasons to prompt for Administration Rights from a BAT file (batch script).

I was getting mad for some time until I found an script to achieve my objective (thanks Stackoverflow) BUT, in the original script any argument/s you pass calling the BAT file was ignored after elevation, so here is my modified version of the script, now argument-compatible :).

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo args = "" >> "%temp%\getadmin.vbs"
    echo For Each strArg in WScript.Arguments >> "%temp%\getadmin.vbs"
    echo args = args ^& strArg ^& " "  >> "%temp%\getadmin.vbs"
    echo Next >> "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", args, "", "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"
:--------------------------------------

Enjoy!

PS: If you share this one, please, refer the source (this blog!). Thanks!