0

enter image description hereI have written a script which Stops/starts the application pool in IIS server, everything is working fine but I am unable to get few statements logged in nested If I have used in the script, Below is script code

set CUR_YYYY=%date:~10,4%
set CUR_MM=%date:~4,2%
set CUR_DD=%date:~7,2%
set LOG_FILE=C:\TEMP\StopStartPool_%CUR_YYYY%%CUR_MM%%CUR_DD%.log 

echo Stopping App Pool: %date% %time% >> %LOG_FILE%
cd %windir%\system32\inetsrv 
appcmd stop apppool /apppool.name:TestMER1
IF %ERRORLEVEL% NEQ 0 (
  echo Error Occurred while stopping pool - %ERRORLEVEL%: %date% %time% >> %LOG_FILE%
  echo Rerunning Stop Command: %date% %time% >> %LOG_FILE% 
  TIMEOUT /T 30
  cd %windir%\system32\inetsrv 
appcmd stop apppool /apppool.name:TestMER
IF %ERRORLEVEL% EQU 0 ( echo Application pool has stopped on rerun >> %LOG_FILE% )
)else ( echo Application pool has stopped: %date% %time% >> %LOG_FILE% )


TIMEOUT /T 60

echo Starting App Pool: %date% %time% >> %LOG_FILE%
cd %windir%\system32\inetsrv 
appcmd start apppool /apppool.name:TestMER1
IF %ERRORLEVEL% NEQ 0 (
  echo Error Occurred while starting pool - %ERRORLEVEL%: %date% %time% >> %LOG_FILE%
  echo Rerunning Start Command: %date% %time% >> %LOG_FILE% 
    TIMEOUT /T 30
  cd %windir%\system32\inetsrv 
appcmd start apppool /apppool.name:TestMER
IF %ERRORLEVEL% EQU 0 ( echo Application pool has Started on rerun >> %LOG_FILE% )
)else ( echo Application pool has Started: %date% %time% >> %LOG_FILE% )

echo -----------------Finished--------------------- >> %LOG_FILE%

Here is a log I am getting in a file,

Stopping App Pool: Tue 04/30/2019 3:18:38.61
Error Occurred while stopping pool - 1168: Tue 04/30/2019 3:18:38.81
Rerunning Stop Command: Tue 04/30/2019 3:18:38.81
Starting App Pool: Tue 04/30/2019 3:20:08.13
Error Occurred while starting pool - 1168: Tue 04/30/2019 3:20:08.41
Rerunning Start Command: Tue 04/30/2019 3:20:08.41

Script is working as expected it just that It is not logging below code result

IF %ERRORLEVEL% EQU 0 ( echo Application pool has stopped on rerun >> %LOG_FILE% )
Biztalker
  • 101
  • 1
  • 2
  • 14
  • What's the value of the errorlevel? – Dominique Apr 30 '19 at 09:54
  • 1
    Do not use `IF %ERRORLEVEL% NEQ 0` or `IF %ERRORLEVEL% EQU 0` because of not working as expected by beginners in batch file writing not knowing [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](https://stackoverflow.com/questions/4094699/) and when and how to use [delayed expansion](https://ss64.com/nt/delayedexpansion.html). Open a cmd window and run `if /?`. The output help explains already on first page the recommended syntax `IF [NOT] ERRORLEVEL number command`. See [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) – Mofi Apr 30 '19 at 09:57
  • 1
    So I recommend to replace all `IF %ERRORLEVEL% NEQ 0` by `IF ERRORLEVEL 1` whereby it is assumed that `appcmd` never exits with a negative value which would be really very uncommon as not recommended on any operating system. Replace `IF %ERRORLEVEL% EQU 0` by `IF NOT ERRORLEVEL 1` or change the order of echo messages. See also [debugging a batch file](https://stackoverflow.com/a/42448601/3074564). Both `else` miss the __required__ space between `)` and `else` and therefore `cmd.exe` exits processing of batch file on reaching __IF__ condition because of a syntax error as output by `cmd.exe`. – Mofi Apr 30 '19 at 10:02
  • 1
    Also `)` on IF condition lines are wrong as the closing `)` must be on same line as `else` where is one more `)` with the missing space between `)` and `else`. See [this answer](https://stackoverflow.com/a/34118487/3074564) for right syntax for __IF__ conditions as well as even more detailed [IF ELSE syntax error within batch file?](https://stackoverflow.com/a/25471786/3074564) – Mofi Apr 30 '19 at 10:08
  • 1
    Have you never read the help text upon typing `if /?`? you will then find examples of `if`/`else` constructs, which do not say `)else (` but `) else (`... – aschipfl Apr 30 '19 at 11:20
  • Whilst it isn't strictly correct, there is generally no issue using `)Else`, as opposed to `) Else`, _`cmd.exe` will automatically add the missing space!_ – Compo Apr 30 '19 at 15:07
  • __Compo__ is absolutely right regarding `)Else` and `) Else`. But code working only because of automatic syntax correction of code by the interpreter is nevertheless not good written code and might not work in future when the interpreter does not anymore correct the syntax automatically. Writing a code 100% correct is always best. – Mofi May 01 '19 at 10:09
  • Thank you all for your responses, – Biztalker May 02 '19 at 11:36
  • I have seen some unexpected behavior here in case of error when **IF %ERRORLEVEL% NEQ 0** executes this statement it get error 1168 and it goes in and run the same command to stop the app pool but here wiered thing is It stops the app pool but again while checking **IF %ERRORLEVEL% EQU 0** it again getting 1168 errorlevel. Can somebody tell me why this is happening ?? thank you ! – Biztalker May 02 '19 at 11:42
  • I have added snapshot of that highlighting what my doubt is – Biztalker May 02 '19 at 11:49
  • @NileshChandane, when you ask a question, it is courteous to follow advice provided in its subsequent comments. You were told not to use `IF %ERRORLEVEL% NEQ 0` and `IF %ERRORLEVEL% EQU 0`, so I would recommend that you follow the advice, modify your code accordingly, and, if after doing so, your code still exhibits issues, [edit your question](https://stackoverflow.com/posts/55917902/edit), to provide the updated code and include further explanation of how it fails to achieve the intended goal. – Compo May 03 '19 at 13:21

0 Answers0