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

问题描述 投票:0回答:2

我正在尝试创建一个批处理文件,该批处理文件调用其他批处理文件,从而在启动时解锁服务器上的特定驱动器。我知道解锁脚本可以正常工作,如果我运行它们,驱动器将按应有的方式解锁,但是当我尝试通过创建另一个批处理脚本以基于服务器名称调用这些脚本来使其自动化时,它将无法解锁。手动运行脚本时,它会告诉我所调用的脚本的解锁码不正确

我很抱歉,这是一个简单的解决方法还是一个愚蠢的问题,我不经常编写脚本,但仍有很多东西要学习。

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

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

我将其更改为

`If %computername% == "Servername" Goto Servername`

我也尝试过将其运行为

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

这可行,但如果我有多行If,则不行

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

Servername.batmanage-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
batch-file command-line bitlocker
2个回答
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)

0
投票

基本误解:

[与许多语言不同,批处理没有“过程”结束的概念-它只是简单地逐行继续执行,直到到达文件末尾。因此,您需要在完成主线后单击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的前三个字符不区分大小写地进行比较,如果找到匹配项,则执行适当的批处理。

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

© www.soinside.com 2019 - 2024. All rights reserved.