Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Event Scripting with WMI
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Event Scripting with WMI
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-05-14

    Table of Contents:
  • Event Scripting with WMI
  • Introduction to WMI
  • Using __InstanceCreationEvent
  • Using __InstanceDeletionEvent

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Event Scripting with WMI - Using __InstanceCreationEvent


    (Page 3 of 4 )

    Next, you need to issue a query using the WMI Service object’s ExecNotificationQuery function.

    Set colEvents = objWMIService.ExecNotificationQuery _

       ("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _

          & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " _

          & "TargetInstance.GroupComponent= " _

          & "'Win32_Directory.Name=""c:\test""'")

    This query probably looks a lot different from the WMI queries to which you are accustomed.  It’s a little complex, but I’ll try to simplify it for you.  Here’s how it translates into English.

    We’re looking for all (SELECT * FROM) creation events (__InstanceCreationEvent), WITHIN 10 seconds, WHERE the returned event (TargetInstance) is a (ISA) file contained in the C:test directory. (The remainder of the query).

    It’s important to note that the WMI query will keep running.  You must stop it by ending the script, otherwise it will continue indefinitely until the WMI Service is stopped.

    Don’t forget that paths must be contained in quotation marks and all backslashes must be escaped with backslashes.

    If you don’t fully understand this query, that’s okay. You can use it as a model for other queries just by changing the folder location.

    So, WMI processes our request and returns a collection of events fitting our description that occurred within the timeout period.

    Now we need to return the results.  We’ll use the NextEvent function provided by the colEvents collection.  Of course, there’s no way of knowing how many times our event occurred so we’ll enclose everything inside of an endless Do…Loop.

    Do While True

       Set objEvent = colEvents.NextEvent()

       WScript.Echo "A new file was just created:", _

          objEvent.TargetInstance.PartComponent

       Exit Do

    Loop

    This Do Loop will continue forever.  I’ve used the Exit Do statement to demonstrate how you can stop this process after one iteration.  You could easily use a conditional statement to exit this as well.

    If you only want to return a specific number of occurrences, try using a For…Next loop instead of the Do…Loop.

    Even now, after ending the Do loop, our WMI query is still running.  You can end it by quitting the script or by setting the objWMIService equal to Nothing if you want the script to continue running.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · Teach your scripts how to react in response to their environment!
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT