[download id=”49″]

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
WITHIN 10
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:

' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution:  Configuration Manager 2007
' // File:      WMIEvent-NewAdvertisement.vbs
' // Author:	Jakob Gottlieb Svendsen, Coretech A/S. https://blog.ctglobalservices.com/jgs
' // Purpose:   Connects to configmgr server, and wait for a new advertisement wmi event.
' //			when a new event is received, the new advertisement is catched and info is printed to screen.
' //			This is for demo of wmi events, and can be used to create scripts that need to handle new advertisements.
' //
' // Usage:     WMIEvent-NewAdvertisement.vbs.vbs
' //
' //
' // CORETECH A/S History:
' // 1.0.0     JGS 24/05/2011  Created initial version.
' // 1.1.0     JGS 28/07/2011  Changed to async behavior by using SWbemSink.
' //
' // Customer History:
' //
' // ***** End Header *****
' //***************************************************************************
'//----------------------------------------------------------------------------
'//
'//  Global constant and variable declarations
'//
'//----------------------------------------------------------------------------

strConfigMgrServer = "CTSCCM01"
strSiteCode = "DK1"

'//----------------------------------------------------------------------------
'//  Main routines
'//----------------------------------------------------------------------------

'Connect to configmgr site
Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!" &_
"\\" & strConfigMgrServer & "\root\SMS\site_" & strSiteCode)

'Create Sink object.
Set objSink = WScript.CreateObject( _
    "WbemScripting.SWbemSink","SINK_")

'Setup WMI event, checking for new advertisement every 10 seconds.
objSWbemServices.ExecNotificationQueryAsync objSink, _
   "SELECT * FROM __InstanceCreationEvent " &_
"WITHIN 10 " &_
"WHERE TargetInstance " &_
"ISA 'SMS_Advertisement' "

While(True)
	'Infinite loop (waiting for events, other code could be executed here.
    WScript.Sleep 1000
Wend 

'New advertisement event received!
Sub SINK_OnObjectReady(wmiObject, wmiAsyncContext)
	Wscript.Echo "An advertisement has been created, running scripts"

    'Debug - This line displays all content of the result. Good for finding the type and properties of the object returned in TargetInstance.
    'WScript.Echo "Received Registry Value Change Event" _
    '& vbCrLf & wmiObject.GetObjectText_() 

    Set objLastItem =  wmiObject.TargetInstance

	Wscript.Echo "-----------------------------------"
	Wscript.Echo "SMS_Advertisement instance"
	Wscript.Echo "-----------------------------------"
	WScript.Echo "ActionInProgress: " & objLastItem.ActionInProgress
	Wscript.Echo "AdvertFlags: " & objLastItem.AdvertFlags
	Wscript.Echo "AdvertisementID: " & objLastItem.AdvertisementID
	Wscript.Echo "AdvertisementName: " & objLastItem.AdvertisementName
	Wscript.Echo "AssignedScheduleEnabled: " & objLastItem.AssignedScheduleEnabled
	Wscript.Echo "AssignedScheduleIsGMT: " & objLastItem.AssignedScheduleIsGMT
	Wscript.Echo "AssignmentID: " & objLastItem.AssignmentID
	Wscript.Echo "CollectionID: " & objLastItem.CollectionID
	Wscript.Echo "Comment: " & objLastItem.Comment
	Wscript.Echo "DeviceFlags: " & objLastItem.DeviceFlags
	Wscript.Echo "ExpirationTime: " & objLastItem.ExpirationTime
	Wscript.Echo "ExpirationTimeEnabled: " & objLastItem.ExpirationTimeEnabled
	Wscript.Echo "ExpirationTimeIsGMT: " & objLastItem.ExpirationTimeIsGMT
	Wscript.Echo "HierarchyPath: " & objLastItem.HierarchyPath
	Wscript.Echo "IncludeSubCollection: " & objLastItem.IncludeSubCollection
	Wscript.Echo "ISVDataSize: " & objLastItem.ISVDataSize
	Wscript.Echo "MandatoryCountdown: " & objLastItem.MandatoryCountdown
	Wscript.Echo "PackageID: " & objLastItem.PackageID
	Wscript.Echo "PresentTime: " & objLastItem.PresentTime
	Wscript.Echo "PresentTimeEnabled: " & objLastItem.PresentTimeEnabled
	Wscript.Echo "PresentTimeIsGMT: " & objLastItem.PresentTimeIsGMT
	Wscript.Echo "Priority: " & objLastItem.Priority
	Wscript.Echo "ProgramName: " & objLastItem.ProgramName
	Wscript.Echo "RemoteClientFlags: " & objLastItem.RemoteClientFlags
	Wscript.Echo "SourceSite: " & objLastItem.SourceSite
	Wscript.Echo "TimeFlags: " & objLastItem.TimeFlags
End Sub

'//----------------------------------------------------------------------------
'//  End Script
'//----------------------------------------------------------------------------

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