批处理文件只能从 C: 驱动器正常运行

批处理文件只能从 C: 驱动器正常运行

我有以下批处理文件,该文件使用 cmd 以管理员身份创建网络共享。当从 c: 驱动器上的任何目录运行时,它会打开 2 个命令提示符窗口并等待用户按下某个键。但是,当从任何其他驱动器运行时,它会快速闪烁 2 个命令提示符窗口,然后消失,并且不会运行该命令。我如何才能让它在任何驱动器上工作?

@echo off
:: BatchGotAdmin
::-------------------------------------
REM  --> Check for permissions
>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"
::--------------------------------------

::ENTER YOUR CODE BELOW:
net share sharename=C:\tada /grant:everyone,FULL

echo ...
echo ...
echo PLEASE CHECK ABOVE IF SHARE WAS SUCCESFUL. YOU MAY NOW CLOSE THE WINDOW(S)
echo ...
echo ...
pause

更新:

从 中的任何子目录运行它都可以C:。从G:运行它也可以。从 运行它G:\Users\myname\Documents\couple_more_subfolders不起作用

答案1

我找到了另一个没有这个问题的脚本。事实证明,如果第一个脚本所在的文件路径中有空格,它就会失败(所以实际上,这与驱动器号无关):

@echo off
if _%1_==_payload_  goto :payload

:getadmin
    echo %~nx0: elevating self
    set vbs=%temp%\getadmin.vbs
    echo Set UAC = CreateObject^("Shell.Application"^)                >> "%vbs%"
    echo UAC.ShellExecute "%~s0", "payload %~sdp0 %*", "", "runas", 1 >> "%vbs%"
    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
goto :eof

:payload
    ::echo %~nx0: running payload with parameters:
    ::echo %*
    echo ---------------------------------------------------
    cd /d %2
    shift
    shift
    net share sharename=C:\tada /grant:everyone,FULL
echo ...
echo ...
echo PLEASE CHECK ABOVE IF SHARE WAS SUCCESFUL. YOU MAY NOW CLOSE THE WINDOW(S)
echo ...
echo ...
pause
goto :eof

相关内容