从当前目录以管理员身份运行批处理文件(.bat)

时间:2017-06-21 12:15:29

标签: windows batch-file cmd administrator

我目前正在为学校编写代码,允许学生在其Windows设备上以管理员身份运行批处理文件,并接收使用网络基础结构所需的程序。

@echo Welcome to the Mossman State High School Additional Features Installer
@pause

@echo Starting the Credentials GUI
@echo Starting PaperCut Installer
@start "" %~dp0BYOx.exe
@start "" %~dp0client-local-install.exe

@echo Creating Printing Directory
@md C:\Printing

@echo Copying Print Mapping Files
@copy "" %~dp0configuration.xml C:\Printing /E /W
@copy "" %~dp0student_printers.xml C:\Printing /E /W
@copy "" %~dp0PrintMap2.exe C:\Printing /E /W

@copy "" %~dp0StudentPrinting.sfx.exe C:\ /E /W

@echo Running Connect Printer Setup
@start "C:\" StudentPrinting.sfx.exe

@echo Please complete the instructions on BYOx and PaperCut to finilize 
onboarding.
@pause

由于StudentPrinting.sfx.exe程序以前的编写方式,并且需要从C:\ Directory运行,因此需要以管理员身份运行。

我最初发现%~dp0参数有效,但是一旦进行了进一步的测试,我发现一旦点击带有空格的计算机名称就失败了,例如。 C:\Users\ComputerOne\Desktop\Auto_Installer有效但C:\Users\Computer One\Desktop\Auto_Installer不适用于计算机和One之间的空间。由于它将被12至18岁的学生使用,他们将无法解决这个问题。

有没有办法让批处理文件以管理员身份运行,但仍然可以从它所在的目录运行?

3 个答案:

答案 0 :(得分:0)

我首先要确保您当前的目录与脚本的目录匹配。 (这样就不需要用%~dp0作为前缀)。似乎还没有充分的理由说明为什么需要使用Start,并且您的Copy命令似乎使用了错误的语法和错误的选项。

@Echo Off

If /I Not "%CD%\"=="%~dp0" CD /D "%~dp0"

Echo Welcome to the Mossman State High School Additional Features Installer
Timeout 5 /NoBreak>Nul

Echo Copying Print Mapping Files
If Not Exist C:\Printing\ (
    Echo Creating Printing Directory
    MD C:\Printing
)
Copy /Y configuration.xml C:\Printing
Copy /Y student_printers.xml C:\Printing
Copy /Y PrintMap2.exe C:\Printing

Echo Copying printer Setup Files
Copy /Y StudentPrinting.sfx.exe C:\

Echo Running Connect Printer Setup
C:\StudentPrinting.sfx.exe

Echo Please complete the instructions on BYOx and PaperCut to finilize onboarding.

Echo Starting the Credentials GUI
BYOx.exe

Echo Starting PaperCut Installer
client-local-install.exe

Echo(
Echo Finished. Press any key to exit...
Timeout -1

我已将订单更改为更合理的订单,如果在运行其他两个可执行文件之前无法运行打印机设置,请相应调整。

答案 1 :(得分:0)

好的,根据我的理解,这是正确的解决方案: 第一个代码块检查admin并提示您接受,如果不是,或者如果您已经,则运行命令AS ADMIN。 然后PUSHD%~dp0确保你在.bat所在的当前目录中工作(在你的情况下是C:\) 然后,如果它与.bat文件位于同一目录中,则只能使用程序名称! (无需指定路径)

将其另存为.bat并尝试根据需要使用您的代码进行调整(请注意我指定的代码,然后将代码放入其中):

@echo off&cls
REM  --> CHECKING FOR ADMIN
    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"
REM -----------------PUT YOUR COMMANDS BELOW THIS LINE-------------------
CLS
PUSHD %~dp0
echo %CD%
echo.
echo HELLO!
echo.
pause

答案 2 :(得分:0)

非常感谢Compo&拉斐尔协助编程。我已在虚拟机上测试了该程序,它按预期工作。

完成的代码如下;

@echo off&cls
    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" 
"%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" 
"%SYSTEMROOT%\system32\config\system"
)

if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > 
"%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 
>> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"

If /I Not "%CD%\"=="%~dp0" CD /D "%~dp0"

echo Welcome to the Mossman State High School Additional Features Installer
timeout 1 /NoBreak>Nul

echo Copying Print Mapping Files
if not exist C:\Printing\ (
    echo Creating Printing Directory
    md C:\Printing
)
copy /Y configuration.xml C:\Printing
copy /Y student_printers.xml C:\Printing
copy /Y PrintMap2.exe C:\Printing

echo Copying Printer Setup Files
copy /Y StudentPrinting.sfx.exe C:\

echo Running Connect Printer Setup
C:\StudentPrinting.sfx.exe

echo Please complete the instructions on BYOx and PaperCut to finilize 
onboarding.

echo Starting the Credentials GUI
start BYOx.exe

echo Starting PaperCut Installer
start client-local-install.exe

echo(
echo Finished. Press any key to exit...
timeout -1