0

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

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

代替

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

我把它改成

If %computername% == "Servername" Goto Servername

我也尝试将其运行为:

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

这有效,但如果我有多行if.

服务器解锁.bat

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

服务器名.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
4

3 回答 3

1

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)
于 2019-10-04T19:10:51.253 回答
1

基本误解:

与许多语言不同,批处理没有“过程”结束的概念——它只是逐行继续执行,直到到达文件结尾。因此,您需要goto :eof在完成主线之后,否则将通过子程序代码继续执行。:EOF是由CMDto mean理解的预定义标签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

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

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

于 2019-10-04T19:42:01.353 回答
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")
于 2019-10-05T10:00:49.733 回答