如何在bat中请求管理员权限


REM  --> Check for permissions

>nul 2>&1 "%SYSTEMROOT%\system32\icacls.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
REM output the command  to getadmin.vbs
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "%1 %2 %3", "", "runas", 1 >> "%temp%\getadmin.vbs"
REM run getadmin.vbs
    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
REM delete the temp file
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"

你可能感兴趣的:(bat,批处理)