How do you run a command as an administrator from the Windows command line?

前端 未结 8 766
眼角桃花
眼角桃花 2020-11-28 19:14

I have a small script that performs the build and install process on Windows for a Bazaar repository I\'m managing. I\'m trying to run the script with elevated, administrati

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 19:47

    A batch/WSH hybrid is able to call ShellExecute to display the UAC elevation dialog...

    @if (1==1) @if(1==0) @ELSE
    @echo off&SETLOCAL ENABLEEXTENSIONS
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"||(
        cscript //E:JScript //nologo "%~f0"
        @goto :EOF
    )
    echo.Performing admin tasks...
    REM call foo.exe
    @goto :EOF
    @end @ELSE
    ShA=new ActiveXObject("Shell.Application")
    ShA.ShellExecute("cmd.exe","/c \""+WScript.ScriptFullName+"\"","","runas",5);
    @end
    

提交回复
热议问题