Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Introduction to WQL: SQL for 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

Introduction to WQL: SQL for WMI
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-05-16

    Table of Contents:
  • Introduction to WQL: SQL for WMI
  • Data Queries
  • Event Queries
  • Schema Queries

  • 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


    Introduction to WQL: SQL for WMI - Event Queries


    (Page 3 of 4 )

    Event Queries

    The second type of query is a little less common, but extremely powerful when used correctly. The Event query creates a temporary event request notification that is flagged each time a new instance of a class is created.

    If we look at the last example, we were polling the Win32_NTLogEvent class for event log entries.  We can use an Event Query to watch that class for any newly created instances as well.

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")

    Set colEvents = objWMIService.ExecNotificationQuery _

       ("SELECT * FROM __InstanceCreationEvent WHERE " _

           & "TargetInstance ISA 'Win32_NTLogEvent' " _

           & "AND TargetInstance.Logfile = 'Application' ")

    The structure of this query is a little more complex, but it’s not difficult.  You’ll find that it’s very similar to the data query we used.

    Again we’re going to SELECT all instances with our wildcard FROM the __InstanceCreationEvent event type.  We’re looking for the request notification returned by our event so the FROM keyword should point to the event, not the class in which the event occurs.

    Then we set up a WHERE clause to filter our results.  This is where it looks a little funny.  Notice how we’re not calling the properties directly this time?  That’s because we can’t reference the class item directly.  We’re only returning event results.  So we use the TargetInstance moniker to refer to the returned event.

    A moniker is like a pointer that cross references items in different classes.

    The ISA is a keyword to refer to the referenced properties.  So our query is looking for all events WHERE the TargetInstance is a member of the Win32_NTLogEvent class AND the TargetInstance’s Logfile property is set to “Application.”

    So this event query would return any newly created events that fit the description in our first example.  The data query returns any existing items when it is run and the event query returns any newly created items after it is run.

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


       · In order to harness the power of WMI in your scripts, you'll need to be able to...
     

    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 5 hosted by Hostway
    Stay green...Green IT