2012-12-07 44 views
0

我创建了一个批处理来运行特定的命令,代码如下所示:作为管理员在批处理中运行设置文件?

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

我想是运行setupc文件作为一个管理员?

我试图runas /user:<Name>\administrator命令,但它没有工作。

有没有简单的方法来做到这一点?

+1

难道你不需要引用那条路吗?它里面有空格。 – Blorgbeard

+0

确保您使用[运行方式(http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/runas.mspx?mfr=true)正确。 –

+0

@Blorgbeard,实际上路径工作,我可以从批处理文件运行。但我想要做的是以管理员身份运行 – Liban

回答

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

谢谢,我会试一试。 – Liban

相关问题