批处理文件似乎无法正确调用其他批处理文件

时间:2019-10-04 17:14:34

标签: batch-file command-line bitlocker

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

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

代替

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

3 个答案:

答案 0 :(得分:1)

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


您需要等式两边的引用都相等

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

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


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

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

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

考虑以下替代脚本

  1. 一组匹配的推送/弹出
  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)

答案 1 :(得分:1)

基本误解:

与许多语言不同,批处理没有“过程”结束的概念-它只是简单地逐行继续执行,直到到达文件末尾。因此,您需要在完成主线后goto :eof,否则将通过子例程代码继续执行。 :EOFCMD理解为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的前三个字符进行比较,如果找到匹配项,则执行适当的批处理。

由于del是一个关键字-是一个危险的关键字,delete我也展示了如何进行异常处理。

答案 2 :(得分:0)

您可以将所有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")