Saved Bookmarks
| 1. |
Solve : help - batch file to send automated email when windows service stops? |
|
Answer» The official recommendation for the Windows scripting engine is to use double slashes for switches intended to be read by the engine (cscript.exe or wscript.exe) and single slashes for options intended to be read by the script. The help visible when you type wscript /? (or more PROPERLY wscript //?) at the prompt shows 2 slashes for the switches available, although one will work in a simple case where the script itself is not supplied with any slashed parameters, as you have noticed.great salmon I am impressed In addition to the above I point out that you can modify various script engine options: you can set the default scripting engine to be either cscript or wscript you can set the default logo behaviour to be //nologo or //logo type either cscript //? or wscript //? at the prompt to see how. Quote from: 29april on December 04, 2009, 02:52:03 PM hi, I need to create a batch file that sends an email automatically when a windows service stops. Another way to approach this would be to monitor the service and send the email when the service status changes to STOPPED. Rather then manually run a batch file every 10 minutes or use the scheduler, this script will run in an infinite loop monitoring the service. Code: [Select]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) = "" 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 = "" 'email body text quoted .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "" 'your ISP SMTP server address .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 .Configuration.Fields.Update .Send End With Set cdo = Nothing End Sub FILL in the information where noted, save script with a VBS extension and run from the command prompt as cscript scriptname.vbs Good luck. Sidewinder, once the script discovers the service is in the stopped state, will it keep sending emails every 10 minutes? sidewinder won't it require password for sending the mails I mean if i add in the field From="[email protected]" then will it send the email without the password here ?? Quote from: Salmon Trout on December 05, 2009, 08:47:22 AM Sidewinder, once the script discovers the service is in the stopped state, will it keep sending emails every 10 minutes? No. The script only sends an email when an event changes the service state to stopped or stop pending. Apparently, services and processes act differently in that processes terminate and flush out of memory while services are forever and may simply stop running. Quote from: Bukhari1986 on December 05, 2009, 09:09:36 AM sidewinder Depends on the ISP. I got it to work as posted, but if a username and password are required (security breach) there are CDO configuration items for just that purpose. The OP never even mentioned the service name and for all we know the email may be going through an internal network where username and password are not required. The link below is old material. It dates back to Windows 98. OLEXP: How to Send Outlook Express Mail from a Command Line I executed the below script but nothing happens. If the service is stopped I get a dialog box with the wscript.echo command, if it is started, nothing happens. emails are not being sent out either (I am able to send independently from command prompt). Is there a way I can spool on the entire script to a log file and find out what the issue is? do you guys see anything wrong with the below script? PLEASE let me know. Thanks for ur support. strComputer = "." Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service") For Each objService in colRunningServices If objService.DisplayName = "Licensing Service" And objService.State = "Stopped" Then WScript.Echo "Licensing Service is Stopped" Call SendMail End If Next Sub SendMail() Set cdo = CreateObject("CDO.Message") With cdo .Subject = "Service Down" 'email subject line quoted .From = "[email protected]" 'your email address quoted .To = "[email protected]" 'recipient email address quoted .TextBody = "Test" 'email body text quoted .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost" 'localhost .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 .Configuration.Fields.Update .Send End With Set cdo = Nothing End SubI couldn't see anything wrong, but I couldn't get it to run either, even with valid values. Question: why is the smtpserver set to localhost? Are you running your own mail server? or are you using an ISP? Code: [Select]strComputer = "." Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colRunningServices = objWMIService.ExecQuery("Select * from Win32_Service") For Each objService in colRunningServices If objService.DisplayName = "Licensing Service" And objService.State = "Stopped" Then WScript.Echo "Licensing Service is Stopped" Call SendMail End If Next Sub SendMail() Set CDO = CreateObject("CDO.Message") With cdo .From = "[email protected]" .To = "[email protected]" .Subject = "Service Down" .Textbody = "Test" .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp mail server" .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 ' .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "userid" ' .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password" ' .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 .Configuration.Fields.Update .Send End With End Sub If using an ISP, check with them for the smtp server name and the port to use (this information is probably on their website). If you need to use authentication, uncomment the three configuration statements and fill in the values appropriately. Yeah, I know having the userid and password in the script is a security breach, but that is whole other subject. Good luck. or maybe there's something wrong with the schema site from M$. |
|