作为管理员批量运行设置文件?

时间:2012-12-07 02:48:32

标签: batch-file cmd

我创建了一个批处理来运行特定命令,代码如下所示:

cd  D:\projects\Project Stress Test\signed one\com0com\x64
setupc
pause

我想要的是以管理员身份运行setupc文件?

我尝试了runas /user:<Name>\administrator命令,但它没有用。

有没有简单的方法呢?

1 个答案:

答案 0 :(得分:3)

您可以使整个脚本在管理级别运行。这是我在脚本中使用的批处理函数。

@echo off
call :Admin xReturn true 1
echo.%xReturn%
goto End

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Admin <Return> [Needed] [Success]
:: Check for Administrator privileges and request privileges if needed.
:: NOTE: This will restart the script with Admin privs if Needed is set to true.
:::: Usage: call :Admin xReturn true
:: Return success value, if user is Admin. Default `true` if Success not set.
setlocal
set xVBUAC=%Temp%\AdminUAC.vbs
set xSuccess=true
if not "%~3"=="" set xSuccess=%~3

:: Check for Access
::net session >nul 2>&1
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if %ErrorLevel% EQU 0 set xAdmin=%xSuccess%

:: Execute UAC
if /i not "%xAdmin%"=="%xSuccess%" if not "%~2"=="" if /i "%~2"=="true" (
  echo Set UAC = CreateObject^("Shell.Application"^) > "%xVBUAC%"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%xVBUAC%"
    if exist "%xVBUAC%" (
        "%xVBUAC%"
        rem if %ErrorLevel% EQU 5 echo Access Denied. Launching UAC.
        del "%xVBUAC%"
    )
)
endlocal & if not "%~1"=="" set "%~1=%xAdmin%"
goto :eof

:End