Link to home
Start Free TrialLog in
Avatar of GMJ29
GMJ29

asked on

Send an email when services stop on Windows 2003

I want to setup an email notification when any services stop on Windows 2003. We don't want to install any third party or free/shareware software. I see in Recovery for the service properties you can run a program if the service fails, but I don't think there is a way to send an email through a batch file. Can someone please point me in the right direction?
Avatar of joedoe58
joedoe58

You are on the right track, you can run a batch file that is triggered when the service fails
Avatar of GMJ29

ASKER

yes, but i don't know of any option to send an email through a batch file...
AFAIK, there's no command-line mail client built-into Windows...(outside of telnetting into port 25 or your mail server and using SMTP commands..) so either way I think you'll need to install something.

I would personally recommend installing 'Servers Alive' on one machine, and you can monitor up to 10 machines with the free version:
http://www.download.com/Servers-Alive/3000-2085_4-10264268.html?tag=lst-0-2
Hi GMJ29,
Below you can see a script (sendmail.bat) which sends email using local SMTP service. Sendmail.bat uses a script file sendmail.vbs.

Create files sendmail.bat and sendmail.vbs and place the script bodies shown below into the appropriate file using a text editor (e.g. notepad.exe). In this example, files should be located in c:\monitoring\  directory.

Now you can just run sendmail.bat "Message body".

=============sendmail.bat=====================
Rem This script can be used for sending email from command prompt
rem using cscript.exe and sendmail.vbs
rem Usage: sendmail.bat "Message body"
c:
cd\
cd\monitoring\
CSCRIPT.EXE SENDMAIL.VBS -t helpdesk@domain.com -f monitor@domain.com -s "Alarm from monitoring system at ABC company" -b %1
rem pause
=============sendmail.bat=====================


=============sendmail.vbs=====================
'--------------------------------------------------
   '
   ' Sends email from the local SMTP service using CDONTS objects
   '
   ' Usage:
   '   sendmail -t <to> -f <from> -s "<subject>" -b "<message>"
   '   sendmail [-help|-?]
   '
   '--------------------------------------------------

   Option Explicit
   On Error Resume Next

   Dim objSendMail, oArgs, ArgNum
   Dim strTo, strFrom, strSubject, strBody

   Set oArgs = WScript.Arguments
   ArgNum = 0

   While ArgNum < oArgs.Count
      Select Case LCase(oArgs(ArgNum))
         Case "-to","-t":
            ArgNum = ArgNum + 1
            strTo = oArgs(ArgNum)
         Case "-from","-f":
            ArgNum = ArgNum + 1
            strFrom = oArgs(ArgNum)
         Case "-subject","-s":
            ArgNum = ArgNum + 1
            strSubject = oArgs(ArgNum)
         Case "-body","-b":
            ArgNum = ArgNum + 1
            strBody = oArgs(ArgNum)
         Case "-help","-?":
            Call DisplayUsage
         Case Else:
            Call DisplayUsage
      End Select
      ArgNum = ArgNum + 1
   Wend

   If oArgs.Count=0 Or strTo="" Or strFrom="" Or _
         strSubject="" Or strBody="" Then
      Call DisplayUsage
   Else
      Set objSendMail = CreateObject("CDONTS.NewMail")
         objSendMail.From = strFrom
         objSendMail.To = strTo
         objSendMail.Subject = strSubject
         objSendMail.Body = strBody
         objSendMail.Send
      Set objSendMail = Nothing
   End If

   ' Display the usage for this script
   Sub DisplayUsage
      WScript.Echo "Usage:"
      WScript.Echo "  sendmail -t <to address> -f <from address> -s " & _
         Chr(34) & "<subject>" & Chr(34) & " -b " & Chr(34) & _
         "<message body>" & Chr(34)
      WScript.Echo "  sendmail [-help|-?]"
      WScript.Echo ""
      WSCript.Quit
   End Sub
=============sendmail.vbs=====================


Cheers!
Avatar of GMJ29

ASKER

Thanks, I tried running this in a command prompt and all it did was show each line of the sendmail.bat script. It didn't actually send an email. What I did was save each of these files in c:\temp and I changed the directory in the .bat file to correspond. I ran: sendmail.bat "Send Message" and nothing happened...
Few checks:
1. Is the smtp service running on the computer where you run the script?
2. Did you modify the -t helpdesk@domain.com -f monitor@domain.com in the sendmail.bat? You have to replace the domain.com with your smtp mail server name (or just domain if you have MX records defined on your DNS server for your domain). Did you replace the monitor@domain.com with an emailbox where you can receive the emails?
3. Can your mail server accept emails from the host where you run the script?

Let me know.
Avatar of GMJ29

ASKER

1. yes
2. yes
3. yes

We're trying to run this on a 2003 machine. I don't believe that cdonts comes installed with 2003. Do you know if this is true? When I run the bat file from command prompt it just spits back each line of code and does nothing. I think it has something to do with the cdonts.
OK you are running W2k3 server. It is correct that CDONTS is not installed on Windows 2003 server by default (Microsoft Article ID : 315197).

Lets use CDO which is part of Windows 2003. The sendmail.vbs will look like this (no changes to sendmail.bat):

============sendmail.vbs==================
'--------------------------------------------------
   '
   ' Sends email from the local SMTP service using CDO objects.
   ' CDONTS is not installed on Windows 2003 by default
   ' and CDO objets are prefered on this platform.
   '
   ' Usage:
   '   sendmail -t <to> -f <from> -s "<subject>" -b "<message>"
   '   sendmail [-help|-?]
   '
   '--------------------------------------------------

   Option Explicit
   On Error Resume Next

   Dim objSendMail, oArgs, ArgNum
   Dim strTo, strFrom, strSubject, strBody

   Set oArgs = WScript.Arguments
   ArgNum = 0

   While ArgNum < oArgs.Count
      Select Case LCase(oArgs(ArgNum))
         Case "-to","-t":
            ArgNum = ArgNum + 1
            strTo = oArgs(ArgNum)
         Case "-from","-f":
            ArgNum = ArgNum + 1
            strFrom = oArgs(ArgNum)
         Case "-subject","-s":
            ArgNum = ArgNum + 1
            strSubject = oArgs(ArgNum)
         Case "-body","-b":
            ArgNum = ArgNum + 1
            strBody = oArgs(ArgNum)
         Case "-help","-?":
            Call DisplayUsage
         Case Else:
            Call DisplayUsage
      End Select
      ArgNum = ArgNum + 1
   Wend

   If oArgs.Count=0 Or strTo="" Or strFrom="" Or _
         strSubject="" Or strBody="" Then
      Call DisplayUsage
   Else
      Set objEmail = CreateObject("CDO.Message")
      objEmail.From = strFrom
      objEmail.To = strTo
      objEmail.Subject = strSubject
      objEmail.Textbody = strBody
      objEmail.Send  
      Set objSendMail = Nothing
End If

   ' Display the usage for this script
   Sub DisplayUsage
      WScript.Echo "Usage:"
      WScript.Echo "  sendmail -t <to address> -f <from address> -s " & _
         Chr(34) & "<subject>" & Chr(34) & " -b " & Chr(34) & _
         "<message body>" & Chr(34)
      WScript.Echo "  sendmail [-help|-?]"
      WScript.Echo ""
      WSCript.Quit
   End Sub
============sendmail.vbs==================

Let me know.
ASKER CERTIFIED SOLUTION
Avatar of Znalost
Znalost

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi GMJ29,
Any luck? Let me know.

Thanks
      Znalost