Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am using the Windows Task Scheduler to run a executable that returns 0 when it runs successfully. However, I would like an email notification if (1)the task fails to run or (2)the return code if anything other than 0.

Is this something the Windows Task Scheduler on Windows Server 2003 can do???

share|improve this question
add comment

2 Answers 2

In my quest to eradicate cmd.exe [grin], here's a Powershell script that should also work for you:

# attempt to run your exe.  iex is an alias for the invoke-expression cmd
iex c:\path_to_exe\myprog.exe

# $? lets us know if the previous command was successful or not
# $LASTEXITCODE gives us the exit code of the last Win32 exe execution
if (!$? -OR $LASTEXITCODE -gt 0) 
{
    $smtpServer = "smtp.mydomain.com"
    $fromAddress = "sender@mydomain.com"
    $toAddress = "recipient@mydomain.com"
    $subject = "FAIL"
    $msgBody = "HEY, YOU GOT PROBLEMS"

    # This block is optional depending on your SMTP server config
    # You need it if your SMTP server requires authentication
    $senderCreds = new-object System.Net.networkCredential
    $senderCreds.UserName = "senderusername"
    $senderCreds.Password = "senderpwd"

    $smtpClient = new-object Net.Mail.SmtpClient($smtpServer)
    $smtpClient.Credentials = $senderCreds
    $smtpClient.Send($fromAddress,$toAddress,$subject,$msgBody)
}
share|improve this answer
    
Do you really see that as a substitute for a simple one liner in a batch file? –  John Gardeniers Nov 13 '09 at 18:01
3  
Yes, as I don't have to also keep a copy of blat or some other mailer around. –  squillman Nov 13 '09 at 18:42
    
Avoiding BLAT is awesome for me. Cheers for this. –  D3vtr0n Apr 12 '12 at 19:05
add comment

One very easy trick would be to wrap the command in a CMD / batch script and use the errorlevel returned and Blat to send email conditionally.

share|improve this answer
    
That's what I have always done in the past when I needed to send error messages about scheduled tasks. –  Sam Nov 13 '09 at 14:59
    
+1 because that's what I have always done, until Powershell came about :) –  squillman Nov 13 '09 at 15:49
    
+1 I've also always done it that way. After seeing the Powershell example I'm not about to change. –  John Gardeniers Nov 13 '09 at 18:07
    
Powershell doesnt require BLAT or additional resources for me, so it's actually better. –  D3vtr0n Apr 12 '12 at 19:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.