More Event Scripting with WMI
(Page 1 of 4 )
In the first article of this series we saw how WMI can be used to build an event driven script. In this segment we will be learning what other events WMI makes available to us. Let’s get started.
Before we begin, let’s take another look at the code from the last article. We’ll be using that as the base for all of the samples in this one.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer _
& "rootcimv2")
Set colEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\test""'")
Do While True
Set objEvent = colEvents.NextEvent()
WScript.Echo "File Deleted:", _
objEvent.TargetInstance.PartComponent
Loop
As we’ve learned, this code watches the C:test folder for file deletions. We connect to the WMI service, issue an event based query, and then process the results in an endless Do…Loop.
The important things to note here are the use of the __InstanceCreationEvent event and the use of the NextEvent function to move through returned events.
We’re going to be using this same construct with our next event. We’ll simply plug in a new event and the code will function in an identical fashion.
We know how to watch for creations. We also know how to watch for deletions. The third event type we’ll be looking at will watch for modifications. Again, our examples will be watching for files, but this could easily be altered to watch for changed registry keys or any other scriptable object.
Next: Using __InstanceModificationEvent >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer