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

前端 未结 8 553
眼角桃花
眼角桃花 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 20:00

    Simple pipe trick, ||, with some .vbs used at top of your batch. It will exit regular and restart as administrator.

    @AT>NUL||echo set shell=CreateObject("Shell.Application"):shell.ShellExecute "%~dpnx0",,"%CD%", "runas", 1:set shell=nothing>%~n0.vbs&start %~n0.vbs /realtime& timeout 1 /NOBREAK>nul& del /Q %~n0.vbs&cls&exit
    

    It also del /Q the temp.vbs when it's done using it.

提交回复
热议问题