[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.
[…] Read the full post and download the script from VBScript: Receiving new advertisement info from ConfigMgr 2007 via WMI Events. […]
[…] Read the full post and download the script from VBScript: Receiving new advertisement info from ConfigMgr 2007 via WMI Events. […]
[…] October 25, 2011 in SCCM 2007, System Center Configuration Manager, Tools and Utilities by Anoop C Nair Tweet Original Post – http://blog.coretech.dk/jgs/vbscript-receiving-new-advertisement-info-from-configmgr-2007-via-wmi-ev… […]
[…] the full post and download the script from VBScript: Receiving new advertisement info from ConfigMgr 2007 via WMI Events. jQuery(document).ready(function($) { […]
Hi there to all, the contents present at this web page are actually amazing for people knowledge, well,
keep up the good work fellows.
These companies are experts and know how to re-establish a build
your precious and essential property after great damages made by
inundation or tempest. On the inside, you must document both structure and content.
If this is done prior to any work related to water
damage in Cincinnati, it will make the process of restoration fast and efficient.
If you would like to obtain a great deal from this post then
you have to apply such strategies to your won website.
I am sure this article haas touched all the internet users, its really really goood post on building uup
neew weblog.
Heya i am for the first time here. I came across this board and I in finding It really helpful
& it helped me out a lot. I’m hoping to give one thing again and help
others like you helped me.
Have a look at my page: local marketing expert;
Jon,
An intriguing discussion is worth comment. There’s
no doubt that that you need to write more on this subject matter, it might not
be a taboo matter but usually folks don’t discuss such subjects.
To the next! Best wishes!!
I know this if off topic but I’m looking into starting my own blog and was curious what all is needed to get set up?
I’m assuming having a blog like yours would cost a pretty
penny? I’m not very internet smart so I’m not 100% sure.
Any suggestions or advice would be greatly appreciated.
Kudos
my weblog … gold ira account