其他分享
首页 > 其他分享> > 如何默认以管理员身份运行 bat 文件

如何默认以管理员身份运行 bat 文件

作者:互联网

  1. 检查权限,无权限退出

    rem 检查是否使用管理员权限
    echo Administrative permissions required. Detecting permissions...
    
    net session >nul 2>&1
    if %errorLevel% == 0 (
       echo Success: Administrative permissions confirmed.
    ) else (
       echo Failure: Current permissions inadequate.
       goto end
    )
    
  2. elevate to admin and also stay in the correct directory. https://stackoverflow.com/questions/6811372/how-to-code-a-bat-file-to-always-run-as-admin-mode

    set "params=%*"
    cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )
    

标签:bat,temp%,getadmin,默认,echo,vbs,管理员,&&,permissions
来源: https://www.cnblogs.com/nehcdahc/p/13068377.html