批处理文件似乎没有正确调用其他批处理文件

Batch file doesn't seem to call other batch files correctly

我正在尝试创建一个批处理文件来调用其他批处理文件,这些文件在启动时解锁服务器上的特定驱动器。我知道解锁脚本有效。如果我 运行 他们,驱动器就会解锁。但是当我尝试通过创建另一个批处理脚本来根据服务器名称调用这些脚本来自动化它时,它无法解锁。当 运行 手动运行脚本时,它告诉我调用的脚本有不正确的解锁代码。

我尝试添加目标服务器的全名。

而不是

If %computername% == "PartialServername*" Goto Servername

我改成了

If %computername% == "Servername" Goto Servername

我也试过 运行将其设置为:

If %computername% == "Servername" Call Servername.bat

这行得通,但如果我有多行 if.

服务器Unlock.bat

pushd "\servername\scripts\unlock scripts\"
If %computerName% == "PartialServerName1*" Goto Servername
:Servername Call Servername.Bat

Servername.bat

manage-bde -unlock d: -recoverypassword *Recovery key here*

我原以为它会调用批处理文件并解锁驱动器,但批处理文件会调用文件然后说恢复密码错误。

完整脚本:

REM Created temporary network drive to the unlock scripts folder on vipre

pushd "\scriptserver\unlock scripts\"

REM Checks the value of the Server name then goes to that section of the 
script REM to run the appropriate script
If %computerName% == "Bow*" Goto Bow
If %computerName% == "Del*" Goto Del
If %computerName% == "Fin*" Goto Fin
If %computerName% == "Gah*" Goto Gah
If %computerName% == "Gib*" Goto Gib
If %computerName% == "Kal*" Goto Kal
If %computerName% == "Ken*" Goto Ken
If %computerName% == "Lei*" Goto Lei
If %computerName% == "Lew*" Goto Lew
If %computerName% == "Lim*" Goto Lim
If %computerName% == "Loa*" Goto Loa
If %computerName% == "Mar*" Goto Mar
If %computerName% == "Ott*" Goto Ott
If %Computername% == "Pem*" Goto Pem
If %computerName% == "Ric*" Goto Ric
If %computerName% == "Sha*" Goto Sha
If %computerName% == "Wes*" Goto Wes
:Bow
Call Bow.bat
Call DistributionRestart.bat
popd
:Del
Call Del.bat
Call DistributionRestart.bat
popd
:Fin
Call Fin.bat
Call DistributionRestart.bat
popd
:Gah
Call Gah.bat
Call DistributionRestart.bat
popd
:Gib
Call Gib.bat
Call DistributionRestart.bat
popd
:Kal
Call Kal.bat
Call DistributionRestart.bat
popd
:Ken
Call Ken.bat
Call DistributionRestart.bat
popd
:Lei
Call Lei.bat
Call DistributionRestart.bat
popd
:Lew
Call Lew.bat
Call DistributionRestart.bat
popd
:Lim
Call Lim.bat
Call DistributionRestart.bat
popd
:Loa
Call Loa.bat
Call DistributionRestart.bat
popd
:Mar
Call Mar.bat
Call DistributionRestart.bat
popd
:Ott
Call Ott.bat
Call DistributionRestart.bat
popd
:Pem
Call Pem.bat
Call DistributionRestart.bat
popd
:Ric
Call Ric.bat
Call DistributionRestart.bat
popd
:Sha
Call Sha.bat
Call DistributionRestart.bat
popd
:Wes
Call Wes.bat
popd

Computername 是一个环境变量,因此比较需要不区分大小写。使用 /I 选项进行不区分大小写的搜索


等式两边的引号要相等

IF/I "%computername%" == "<value>" ....
.    ^              ^    ^       ^
.    ^              ^    ^       ^

注意 %computername% 和 <value> 周围的引号。


通配符逻辑不起作用,但一个简单的技术是使用子字符串比较。您总是比较 3 个字符,因此您可以将逻辑更改为:

if /I "%COMPUTERNAME:~0,3%" == "Bow" .....

这从开头(索引 0)开始并创建一个长度为 3 的字符串,然后将其与 Bow 进行比较以进行不区分大小写的字符串比较,其中引号在两边匹配

考虑以下替代脚本

  1. 一组匹配pushd/popd
  2. 使用辅助函数withDistribRestartnoDistribRestart触发相应的脚本,其中传入的参数是要执行的批处理脚本的名称
  3. 检查并报告意外的计算机名称

@ECHO OFF
GOTO :Main


REM =========================================================================
:Main
SETLOCAL
    SET "retVal=0"


    pushd "\scriptserver\unlock scripts"


    if /I "%COMPUTERNAME:~0,3%" == "Bow" (
        CALL :withDistribRestart Bow
    ) else if /I "%COMPUTERNAME:~0,3%" == "Del" (
        CALL :withDistribRestart Del
    ) else if /I "%COMPUTERNAME:~0,3%" == "Fin" (
        CALL :withDistribRestart Fin
    ) else if /I "%COMPUTERNAME:~0,3%" == "Gah" (
        CALL :withDistribRestart Gah
    ) else if /I "%COMPUTERNAME:~0,3%" == "Gib" (
        CALL :withDistribRestart Gib
    ) else if /I "%COMPUTERNAME:~0,3%" == "Kal" (
        CALL :withDistribRestart Kal
    ) else if /I "%COMPUTERNAME:~0,3%" == "Ken" (
        CALL :withDistribRestart Ken
    ) else if /I "%COMPUTERNAME:~0,3%" == "Lei" (
        CALL :withDistribRestart Lei
    ) else if /I "%COMPUTERNAME:~0,3%" == "Lew" (
        CALL :withDistribRestart Lew
    ) else if /I "%COMPUTERNAME:~0,3%" == "Lim" (
        CALL :withDistribRestart Lim
    ) else if /I "%COMPUTERNAME:~0,3%" == "Loa" (
        CALL :withDistribRestart Loa
    ) else if /I "%COMPUTERNAME:~0,3%" == "Mar" (
        CALL :withDistribRestart Mar
    ) else if /I "%COMPUTERNAME:~0,3%" == "Ott" (
        CALL :withDistribRestart Ott
    ) else if /I "%COMPUTERNAME:~0,3%" == "Pem" (
        CALL :withDistribRestart Pem
    ) else if /I "%COMPUTERNAME:~0,3%" == "Ric" (
        CALL :withDistribRestart Ric
    ) else if /I "%COMPUTERNAME:~0,3%" == "Sha" (
        CALL :withDistribRestart Sha
    ) else if /I "%COMPUTERNAME:~0,3%" == "Wes" (
        CALL :noDistribRestart Wes
    ) else (
        ECHO Unexpected COMPUTERNAME '"%COMPUTERNAME%"'
        SET "retVal=1"
    )

    popd
(ENDLOCAL
 EXIT /B %retVal%)



REM ========================================================================
:withDistribRestart
SETLOCAL
    SET "PREFIX=%~1"

    CALL "%PREFIX%.bat"
    CALL "DistributionRestart.bat"
(ENDLOCAL
 EXIT /B 0)




REM ========================================================================
:noDistribRestart
SETLOCAL
    SET "PREFIX=%~1"

    CALL "%PREFIX%.bat"
(ENDLOCAL
 EXIT /B 0)

基本误解:

与许多语言不同,批处理没有 "procedure" 结尾的概念 - 它只是继续逐行执行,直到到达文件末尾。因此,您需要在完成主线后 goto :eof ,否则将继续执行子程序代码。 :EOF 是一个预定义的标签,CMD 理解为 end of file。冒号 必填.

for %%a in (Bow Del Fin ....) do If /i "%computerName:~0,3%" == "%%a" (
 if /i "%%a" == "Del" (
  Call delsub.bat
  Call DistributionRestart.bat
 ) else (
  Call %%a.bat
  Call DistributionRestart.bat
 )
)
popd

此处,列表中的每个值都与 computername 的前 3 个字符进行不区分大小写的比较,如果找到匹配项,则执行相应的批处理。

由于 del 是一个关键字 - 并且是一个危险的关键字,delete 我还展示了如何进行例外处理。

您可以将所有这些 If 命令替换为:

GoTo %ComputerName:~,3% 2>NUL||Exit /B

不过,您也可以在一行中完成所有脚本:

@PushD "\scriptserver\unlock scripts" 2>NUL&&For %%A In (Bow Del Fin Gah Gib Kal Ken Lei Lew Lim Loa Mar Ott Pem Ric Sha Wes)Do @If /I "%ComputerName:~,3%"=="%%A" (Call "%%A.bat"&If /I Not "%%A"=="Wes" Call "DistributionRestart.bat")

为了便于阅读,您当然可以将其分成更多行:

@Echo Off
PushD "\scriptserver\unlock scripts" 2>NUL||Exit /B
For %%A In (Bow Del Fin Gah Gib Kal Ken Lei Lew Lim Loa Mar Ott Pem Ric Sha Wes
)Do If /I "%ComputerName:~,3%"=="%%A" (Call "%%A.bat"
    If /I Not "%%A"=="Wes" Call "DistributionRestart.bat")