Link to home
Start Free TrialLog in
Avatar of Jay Thomas
Jay ThomasFlag for United Kingdom of Great Britain and Northern Ireland

asked on

script to send an email alert when a service fails

Hi, got a server behind  a firewall, I can't monitor it however it does have access to send SMTP messages . I need a script that sends an email alert support@company.com when and if a service called mccaservice fails.

I'm not a script guy so don't know where to start. Any help much appreciated.

Thanks
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, try something like this.  This script, when started, will run until the computer is rebooted, or the service has failed, at which point it will send an email, and stop the monitoring, so you'll need to start it up again once you've fixed the service.

Regards,

Rob.
strComputer = "."
strService = "mccaservice"

' Email variables:
strServer = "mailhost.abc.com"
strTo = "john.doe@abc.com"
strFrom = "john.doe@abc.com"
strSubject = "Service Failed"
strBody = strService & " is not running." & VbCrLf

Set objService = GetObject("WinNT://" & strComputer & "/" & strService & ",Service")
boolEnd = False
Do
	WScript.Sleep 5000   ' Wait for 5 seconds
	If objService.Status <> 4 Then
		SendEmail strServer, strTo, strFrom, strSubject, strBody, ""
		boolEnd = True
	End If
Loop Until boolEnd = True

Sub SendEmail(strServer, strTo, strFrom, strSubject, strBody, strAttachment)
        Dim objMessage
        
        Set objMessage = CreateObject("CDO.Message")
        objMessage.To = strTo
        objMessage.From = strFrom
        objMessage.Subject = strSubject
        objMessage.TextBody = strBody
  		If strAttachment <> "" Then objMessage.AddAttachment strAttachment
  		
        '==This section provides the configuration information for the remote SMTP server.
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        'Name or IP of Remote SMTP Server
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer
        'Server port (typically 25)
        objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25      
        objMessage.Configuration.Fields.Update
        '==End remote SMTP server configuration section==
 
        objMessage.Send
        Set objMessage = Nothing
End Sub

Open in new window

Avatar of Jay Thomas

ASKER

Hi Rob, that works brilliantly my friend. I have added it to a schedule job to keep it running after server reboots etc. Can i ask 2 questions. 1. whats does this do:
WScript.Sleep 5000   ' Wait for 5 seconds
      If objService.Status <> 4 Then
            SendEmail strServer, strTo, strFrom, strSubject, strBody, ""
            boolEnd = True
2. How can i onclude 2 email addresses to send to?

Thanks mate.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

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, thanks for the explanation, makes sense. And the 2 addresses works as well.

Thanks very much mate.