?
Solved

Send an email when services stop on Windows 2003

Posted on 2005-04-19
10
Medium Priority
?
550 Views
Last Modified: 2010-04-18
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?
0
Comment
Question by:GMJ29
10 Comments
 
LVL 9

Expert Comment

by:joedoe58
ID: 13818126
You are on the right track, you can run a batch file that is triggered when the service fails
0
 

Author Comment

by:GMJ29
ID: 13818217
yes, but i don't know of any option to send an email through a batch file...
0
 
LVL 16

Expert Comment

by:JammyPak
ID: 13818392
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 2

Expert Comment

by:Znalost
ID: 13818651
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
 

Author Comment

by:GMJ29
ID: 13824162
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
 
LVL 2

Expert Comment

by:Znalost
ID: 13829907
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
 

Author Comment

by:GMJ29
ID: 13833196
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
 
LVL 2

Expert Comment

by:Znalost
ID: 13837425
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
 
LVL 2

Accepted Solution

by:
Znalost earned 2000 total points
ID: 13837750
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
 
LVL 2

Expert Comment

by:Znalost
ID: 13890608
Hi GMJ29,
Any luck? Let me know.

Thanks
      Znalost
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin In this article I will be guiding through the process of removing a failed DC metadata from Active Directory (hereafter, AD) using the ntdsutil tool in a Windows Server 2003 environment. These steps are not necessary in a Win…
Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question