Jump to content

Need to send mail when windows service service stops


windows1711
 Share

Recommended Posts

Hi,



I need to monitor a service in windows server and when it stops need to send an email.



I got the batch file script



strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colMonitoredProcesses = objWMIService. _

ExecNotificationQuery("Select * From __InstanceModificationEvent " _

& " Within 1 where TargetInstance ISA 'Win32_Service'")


Do While True

Set objLatestProcess = colMonitoredProcesses.NextEvent

If LCase(objLatestProcess.TargetInstance.Name) = "Tomcat6" Then 'Name of service quoted; do not use display name

If objLatestProcess.TargetInstance.State = "Stop pending" Then

Call SendMail

End if

If objLatestProcess.TargetInstance.State = "Stopped" Then

Call SendMail

End if

End if

Loop


Sub SendMail()

Set cdo = CreateObject("CDO.Message")

With cdo

.Subject = "Service Down" 'email subject line quoted

.From = "" 'your email address quoted

.To = "" 'recipient email address quoted

.TextBody = "Tomcat Service Down, please start the service" 'email body text quoted


.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "" 'your ISP SMTP server address


.Configuration.Fields.Update

.Send

End With

Set cdo = Nothing

End Sub




When i ran this batch file it throwed the below error, could you please help me to resolve this.


'strComputer' is not recognized as an internal or external command,

operable program or batch file.


d:\>Set objWMIService = GetObject("winmgmts:" _

& was unexpected at this time.


d:\> & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\c

imv2")




Thanks,




Link to comment
Share on other sites

Welcome to the Windows Forum!



What language is that script in? I do not recognize the language.



Anyway, the first error...


You use 'Set' to assign values throughout the script, but not in the case of strComputer = "."



As for the next error, what is that underscore at the end of the line - some kind of continuation mark?


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy