Download “” – Downloaded 329 times – 2 KB

Sometimes you might need to execute code when a new advertisement is created.

This can be pretty easily obtained by using WMI events.

WMI events is supported , as far as I know, by all scripting and programming languages that supports WMI.

To setup a WMI event, you need to make a WMI Event Query. This query describes what to wait for, where to look and how often to check.

To check for new advertisements we will need to use a query simular to this one:

SELECT * FROM __InstanceCreationEvent
WHERE TargetInstance
ISA ‘SMS_Advertisement’

__InstanceCreationEvent describes  that we want the event to fire when a new instance is created. you have other options here, such as __InstanceModificationEvent.

WITHIN 10 tells the system to check every 10 seconds. this can be tuned to your needs.

last part we can modify is the type of object to look for.

ISA ‘SMS_Advertisement’ makes sure that this event fires when a SMS_Advertisement object is created. Of cause you can use this script and method for other types of objects too.

When using VBScript we are able to utilize a Swbem object called “WbemScripting.SWbemSink“. this object support and asynchronous call to vbscript events.

When the object is created, we give it a name, in this case it is “SINK_” .

We call the method ExecNotificationQueryAsync , sending the sink and the query as the input. This means that when the event fires, a sub called SINK_OnObjectReady will be called, since we named our sink “SINK_”.

In this sub we are able to catch the object that was created and use it for whatever purpose we want. In this demo we output all the content to the screen, but other ideas might be to grab the users affected and refresh their machine policy, to make sure that the advertised software will be deployed as quick as possible.

here is the source code for the script:

I hope you find this post useful, don’t hesitate to bring your feedback by email or comment.