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?
GMJ29Asked:
Who is Participating?
 
ZnalostConnect With a Mentor Commented:
I made a mistake in the last version of the script. Here is the verified/correct version:

============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.
   ' Supported platforms: Windows 2003, 2000, XP, NT 4.0, 98
   '
   ' Usage:
   '   sendmail -t <to> -f <from> -s "<subject>" -b "<message>"
   '   sendmail [-help|-?]
   '
   '--------------------------------------------------

   Option Explicit
   On Error Resume Next

   Dim objEmail, 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 objEmail = 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==================
0
 
joedoe58Commented:
You are on the right track, you can run a batch file that is triggered when the service fails
0
 
GMJ29Author Commented:
yes, but i don't know of any option to send an email through a batch file...
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
JammyPakCommented:
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
0
 
ZnalostCommented:
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!
0
 
GMJ29Author Commented:
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...
0
 
ZnalostCommented:
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.
0
 
GMJ29Author Commented:
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.
0
 
ZnalostCommented:
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.
0
 
ZnalostCommented:
Hi GMJ29,
Any luck? Let me know.

Thanks
      Znalost
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.