I put together a script that combined the services change of state script
from here:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0903.mspx
with an e-mail sending script from here:
http://www.paulsadowski.com/WSH/cdo.htm
The script seems to run fine. If i start up the script and then go and stop
a service a message will be sent. And if i do it for multiple servies
multiple messages will be sent. Or at least I think they are getting sent.
The problem is that as far as I can tell the messages sort of queue up and
will only actually send when i close the script. If i don't close the script
i never get the e-mails. The moment I close the script all the e-mails show
up in my e-mail box all at once.
'----#Script:
strComputer = "."
Set objMessage = CreateObject("CDO.Message")
'-----Configure for remote mail 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") = "cokmail1"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'-----End Configure
objMessage.Sender = "e-mail@address.com"
objMessage.To = "e-mail@address.com"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService. _
ExecNotificationQuery("Select * from __InstanceModificationEvent " _
& "within 30 where TargetInstance isa 'Win32_Service'")
i = 0
Do While i = 0
Set objService = colServices.NextEvent
If objService.TargetInstance.State <> _
objService.PreviousInstance.State Then
objMessage.Subject = "Service State Change: " &
objService.TargetInstance.Name
objMessage.TextBody = "The service, " & objService.TargetInstance.Name & ",
has changed state." _
& "The current state is " & objService.TargetInstance.State & ". It was
previously " & objService.PreviousInstance.State & "."
objMessage.Send
End If
Loop
'--------#End Script
Any help would be greatly appreciated.