以管理员身份运行批处理

时间:2015-06-08 16:35:45

标签: windows batch-file cmd

我想要一个以管理员身份运行的批处理文件,现在我发现了以下内容:

:: ### START UAC SCRIPT ###

if "%2"=="firstrun" exit
cmd /c "%0" null firstrun

if "%1"=="skipuac" goto skipuacstart

:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B

:gotPrivileges

setlocal & pushd .

cd /d %~dp0
cmd /c "%0" skipuac firstrun
cd /d %~dp0

:skipuacstart

if "%2"=="firstrun" exit

:: ### END UAC SCRIPT ###

:: ### START OF YOUR OWN BATCH SCRIPT BELOW THIS LINE ###

问题是,我在命令提示符中遇到很多错误,这就是我得到的:

  

C:\ Users \ Mark \ Desktop> if" firstrun" ==" firstrun"出口

     

C:\ Users \ Mark \ Desktop> if"" ==" skipuac"转到skipuacstart

     

C:\ Users \ Mark \ Desktop> NET FILE 1> NUL 2> NUL

     

C:\ Users \ Mark \ Desktop> if' 2' ==' 0' (转到gotPrivileges)其他(转到   getPrivil eges)

     

C:\ Users \ Mark \ Desktop> if'' ==' ELEV' (shift& goto gotPrivileges)

     

C:\ Users \ Mark \ Desktop> setlocal DisableDelayedExpansion

     

C:\用户\马克\桌面>设置   " batchPath = C:\用户\马克\桌面\ Testing.bat"

     

C:\ Users \ Mark \ Desktop> setlocal EnableDelayedExpansion

     

C:\ Users \ Mark \ Desktop> ECHO设置UAC = CreateObject(" Shell.Application")   1>" C:\ us ers \ Mark \ AppData \ Local \ Temp \ OEgetPrivileges.vbs"

     

C:\ Users \ Mark \ Desktop> ECHO UAC.ShellExecute"!batchPath!"," ELEV","",   " runas",1 1>>" C:\ Users \ Mark \ AppData \ Local \ Temp \ OEgetPrivileges.vbs"

     

C:\用户\马克\桌面和GT;" C:\用户\马克\应用程序数据\本地\ TEMP \ OEgetPrivileges.vbs"

有没有办法解决这个问题,只检查管理员权限并要求它没有?

2 个答案:

答案 0 :(得分:0)

将@echo放在第一行是解决方案。

    @echo off 
:: ### START UAC SCRIPT ###

if "%2"=="firstrun" exit
cmd /c "%0" null firstrun

if "%1"=="skipuac" goto skipuacstart

:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B

:gotPrivileges

setlocal & pushd .

cd /d %~dp0
cmd /c "%0" skipuac firstrun
cd /d %~dp0

:skipuacstart

if "%2"=="firstrun" exit

:: ### END UAC SCRIPT ###

:: ### START OF YOUR OWN BATCH SCRIPT BELOW THIS LINE ###

答案 1 :(得分:0)

您可以使用Vbscript来运行它。

Help = VBS

您可以使用以下命令= fltmc来检查您是admin还是No。

这是我的方式:

@echo off
fltmc>nul && Goto :HAccess
Title NAccess
set S=%temp%\getadmin.vbs
echo Set UAC = CreateObject^("Shell.Application"^) > "%S%"
echo UAC.ShellExecute "%~fs0", "", "", "runas", 1 >> "%S%"
cscript //nologo "%S%"
del "%S%"
exit /b

:HAccess
Echo Access !!