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

来源:互联网 发布:做数学题的软件 编辑:程序博客网 时间:2024/05/29 19:41

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"
0 0