如何使用IF Exist条件

时间:2017-09-10 20:16:19

标签: windows batch-file

我有一个批处理脚本来检查任务调度程序运行的任务的状态,批处理文件的状态以及网络负载均衡器的状态。

我的问题是: -

步骤1: - 我想检查在任务调度程序中运行的任务(health.bat)的状态,如果它已启用或处于就绪状态,则跳转并检查步骤2

步骤2: - 这里我需要检查网络负载均衡器(NLB)的状态,如果它处于收敛状态然后跳转并退出或运行 - > health.bat - >如果没有错误 - >启动NLB或者 - >发送带有错误消息的邮件

此代码出现问题: -

代码正在运行以调用health.bat然后它会跳转以启动NLB,即使health.bat失败: -

  @echo off
setlocal enableextensions enabledelayedexpansion
set itemail="mailid"
FOR /F "usebackq" %%i IN (`hostname`) DO SET host=%%i
::set host=%COMPUTERNAME%
set TASKNAME1=health
c:
cd\
cd batch
:: Checking is wscheck is disabled
schtasks.exe /query  /tn "%TASKNAME1%" >CheckTS.txt 2>&1  
findstr "Running Ready" CheckTS.txt
if %errorlevel% NEQ 0 ( 
schtasks.exe /change /ENABLE /TN "%TASKNAME1%" >CheckTS.txt
schtasks.exe /query  /tn "%TASKNAME1%" >>CheckTS.txt
wmailto %itemail% -s"The Task health has been enabled on %host%." -tCheckTS.txt
)
:: Checking if the node is stared
NLB.exe query >CheckNLB.txt 2>&1
findstr /L /c:"converged" CheckNLB.txt
if %errorlevel% NEQ 0 (
call wscheck.bat
IF EXIST findstr /L /c:"<--FAIL-->" health.txt ( 
wmailto %itemail% -s"DOWN The node %host% cannot be started as the health failed." -thealthstatus.txt
goto :exit
) ELSE (
NLB.exe start >CheckNLB.txt
TIMEOUT /T 10
NLB.exe query >>CheckNLB.txt
wmailto %itemail% -s"The NLB has been Started on %host%." -tCheckNLB.txt
)
)


:exit
pause

---&GT; health.bat&LT; ---

@echo off
setlocal enableextensions enabledelayedexpansion
REM set 
set itemail= "mailid"

pushd "c:\health" || goto :eof

    set "tries=2"
    for /l %%a in (1 1 %tries%) do (
      >health.txt (
            call pcheck.cmd
            Tester.exe -s parse 
        )
        echo The Tester returned errorlevel : !errorlevel! 
        if not errorlevel 1 (
            wmailto %itemail% -s"UP -  Health Monitoring." -thealth.txt"
            goto :eof
        )
        if %%a equ %tries% (
           wmailto %itemail% -s"DOWN -  Health Monitoring." -thealth.txt

        )
    )

0 个答案:

没有答案