0

Below is my code i am using right now, it is working but the second if command is reporting the down when there is no down, please help me and point out where i did mistake,

echo on
setlocal
set itemail= "mail id"
C:
cd\
cd batch
Call utltest.cmd >status.txt
ServerTester.exe -services acgenpszrwbklquf  >>status.txt
if %errorlevel%==0 (
wmailto mailid -s"UP -  Health Monitoring." -tstatus.txt"
) else (
c:
cd\
cd batch
Call utltest.cmd >status.txt
ServerTester.exe -services acgenpszrwbklquf  >>status.txt
)
if %errorlevel%==0 (
wmailto mailid -s"UP -  Health Monitoring." -tstatus.txt"
) else (wmailto %itemail% -s"DOWN - WebServices Health Monitoring." -tstatus.txt
)

:eof
T.Anand
  • 463
  • 2
  • 6
  • 19

1 Answers1

1

What your code does is

check server
if errorlevel == 0 (
   send mail
) else (
   check server
)

if errorlevel == 0

and here, in the second errorlevel check there is an ambiguity: you don't know if the errorlevel value that you are checking has been generated by the send mail or the check server

Simplify, don't duplicate code. Just loop over the test checking for sucess. If we reach the limit of checks inform of the error

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "itemail=mail id"

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

    set "tries=2"
    for /l %%a in (1 1 %tries%) do (
        >status.txt (
            call utltest.cmd
            ServerTester.exe -services acgenpszrwbklquf
        )

        echo The ServerTester returned errorlevel : !errorlevel!

        if not errorlevel 1 (
            wmailto "%itemail%" -s"UP -  Health Monitoring." -tstatus.txt
            goto :eof
        )
        if %%a equ %tries% (
            wmailto "%itemail%" -s"DOWN - WebServices Health Monitoring." -tstatus.txt
        )
    )

To avoid problems with delayed expansion, the code uses if errorlevel n. This way we are not reading the value in the variable (%errorlevel%), but using the available if syntax to check against the current errorlevel.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • This very nice and working good, but one thing is we have to consider the error code 1 as well, because our tester will return different value when there is a fail, so i have to change one line logic ( if not errorlevel 1), i have tested with differet combinations( if errorlevel == 0, if errorlevel equ 0, if %%a equ 0 ) but no luck!!!, can you please guide me to edit this logic – T.Anand Oct 14 '16 at 01:50
  • @T.Anand, `if errorlevel n` is evaluated to true for any errorlevel value *greater than or equal* to `n`. So, `if not errorlevel 1` means *"if the errorlevel is not at least 1"* that is, *"if the errorlevel is 0 or less"*. If you want to check for different values, use `if errorlevel n` from greater to lower values. – MC ND Oct 14 '16 at 05:36
  • Thank you so much!!! but generally if not means " if that is not true" right and also my concern is here we havent used any compare-op like(EQU, NTQ, GTQ,GTR) then how the script will assume like "if the errorlevel is 0 or less" for "if not errorlevel 1" this is my confusion. – T.Anand Oct 14 '16 at 06:13
  • @T.Anand as said, `if errorlevel 1` does not check if the error level is 1, it checks if the error level is at least 1 (it can be 27 and the condition will be true). So, the negation of `>=1` is `<1`, that is, `<=0` – MC ND Oct 14 '16 at 06:18
  • it displays errorlevel =0 for all type of failures!! – T.Anand Oct 15 '16 at 11:30
  • Read [here](http://stackoverflow.com/a/30177832/2861476) to understand *why* you are failing to retrieve the value in `errorlevel`. Also, I've included in the code a way to solve it (changes in 2nd and 15th lines) – MC ND Oct 15 '16 at 12:47
  • I want to understand what the exact error so that i can troubleshoot the issue that is the reason i want to display the %errorlevel% – T.Anand Oct 15 '16 at 13:04
  • Yes because of delayedexpansion i am failing to retrieve the value thanks for making me understand as i am beginner but very much interested to learn the batchscripting please help me, now i have tested the code with enabledelayedexpansion but no luck, i have used %errorlevel% and also !errorlevel! below is the result..... SOAP : <-- FAIL -->0.2184042 secs The ServerTester returned errorlevel : 0 The ServerTester returned errorlevel : 0 – T.Anand Oct 15 '16 at 13:04
  • i think you had changed the line2 and line11 now, it worked for me !!!. is this is the way to solve ? can i use it – T.Anand Oct 15 '16 at 14:21
  • @T.Anand ,for me it is not 11 but 15 (blank or not a line is a line), but yes. When inside a block if you want to retrieve the value from a variable changed inside the same block, delayed expansion is the easiest way of doing it. – MC ND Oct 15 '16 at 14:47
  • @T.Anand, Did you read [the previous link](http://stackoverflow.com/a/30177832/2861476)? `!errorlevel!` is the same that `%errorlevel%` In both cases we are refering to the same variable, **but** the `!var!` syntax changes **when** (in what moment) the batch parser retrieves the value from the variable. – MC ND Oct 15 '16 at 16:38
  • this script was working fine, recently i found that if all the service failed it is not sending the mail alert either UP or DOWN, – T.Anand Nov 26 '17 at 18:15