Perm Event Scripting to monitor service state change

Discussion in 'Scripting' started by Michael McGuire, Jul 24, 2003.

  1. I'm trying to learn event scripting and would like to setup a permanent
    event to monitor Service state changes on our team servers. I've put the
    following script together from samples which runs successfully but doesn't
    seem to work, since I can start and stop a services but don't get an email.
    I've confirmed I can telnet to 25 on Smarthost and I'm testing this on a
    Windows XP SP1 system. Any ideas?



    ' Service Event Filter

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" &
    "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    strFilterQuery = "Select * from __InstanceModificationEvent within 1 " &
    "where TargetInstance isa 'Win32_Service'"

    Set objFilterClass = objWMIService.get("__EventFilter")

    Set objFilter = objFilterClass.SpawnInstance_

    objFilter.Name = "ServiceMonitorFilter"

    objFilter.QueryLanguage = "wql"

    objFilter.Query = strFilterQuery

    objFilter.Put_



    ' Service Event Consumer

    Set objConsumerType = objWMIService.get("SMTPEventConsumer")

    Set objConsumer = objConsumerType.SpawnInstance_

    objConsumer.Name = "ServiceMonitorConsumer"

    objConsumer.Message = "A service has changed state."

    objConsumer.SMTPServer = "Smarthost"

    objConsumer.Subject = "Service state change"

    objConsumer.ToLine = ""

    objConsumer.Put_



    ' Filter to Consumer Bind

    Set objBindingClass = objWMIService.Get("__FilterToConsumerBinding")

    Set objBindingInstance = objBindingClass.SpawnInstance_



    objBindingInstance.Filter = "ServiceMonitorFilter"

    objBindingInstance.Consumer = "ServiceMonitorConsumer"



    objBindingInstance.Put_
     
    Michael McGuire, Jul 24, 2003
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.