1
static void Main(string[] args) 
{
    Process process=Process.Start(@"C:\Users\dalvi\Desktop\iisresetdaily.bat");

    if(process.ExitCode==0)
        SendMail("Sucesss in iisreset..", "bye"); // --> case 1
    else
        SendMail("Failed iisreset..", "bye"); // --> case 2
}

Edit : My SendMail method is working fine.

I've written iisresetdaily.bat file which is doing iisreset /stop and iisreset /start. When it executes successfully, I need to send success in iisrest mail --> case 1, otherwise case 2.

But when I edit the .bat file and put some random words so batch file is failing, but still it's sending case 1 mail meaning success in iisreset.

Am I doing wrong to check status of batch file using Process.ExitCode here?

If batch file failed, I need to send case 2 failed iisreset.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155

4 Answers4

0

That is because your "batch file" may not be able to reset IIS or whatever buts it does complete execution properly and exit with success [ie statusCode=0].

Try to make your batch like:

@echo Started
--Some code that works
--Some code that surely fails [But not break the batch, as in calling external program]
@echo Finished

You will notice that though "Some code that surely fails", you still witness "Finish".

Kartikya
  • 455
  • 2
  • 9
0

I believe the problem is that the .bat script is not providing any useful error code / exit code for you to check. This question might give you some useful information about how to set it up correctly.

Also, see this question for info on terminating a batch when encountering an error.

Community
  • 1
  • 1
Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

You would need to wait the process for exit before you can use ExitCode:

using(var process=Process.Start(@"iisresetdaily.bat")) {
    process.WaitForExit();

    var message=
        0!=process.ExitCode
            ?"Failed iisreset .."
            :"Sucesss in iisreset ..";

    SendMail(message, "bye"); 
}

It would be better if you can provide the content of the batch file, that we can help to figure out why.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
0

Are you checking iisreset exit codes in the batch script and propagating them?

According to http://support.microsoft.com/kb/202013they have an example showing that it will return an error code of 1 if it fails.

@echo off
IISRESET /STOP /NOFORCE
if errorlevel == 1 goto EXIT
copy %systemroot%\system32\LogFiles\W3SVC1 d:\backup\W3SVC1
IISRESET /START
:EXIT
NStuke
  • 1,001
  • 8
  • 11