您可以使整个脚本在管理员级别运行。这是我在脚本中使用的批处理函数。
@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
难道你不需要引用那条路吗?它里面有空格。 – Blorgbeard
确保您使用[运行方式(http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/runas.mspx?mfr=true)正确。 –
@Blorgbeard,实际上路径工作,我可以从批处理文件运行。但我想要做的是以管理员身份运行 – Liban