Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - More 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

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

    Table of Contents:
  • More Event Scripting with WMI
  • Using __InstanceModificationEvent
  • Parsing WMI’s results
  • Using __InstanceOperationEvent

  • 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


    More Event Scripting with WMI - Using __InstanceModificationEvent


    (Page 2 of 4 )

    We’ll be using the __InstanceModificationEvent event.  This event watches an object for any modifications.  These include: changing the object name, adding or removing content, or altering any attributes.  Essentially, it watches for any time the object is changed or modified in any way.

    This is useful for watching a file or folder for updates, or watching specific registry keys for unwanted changes.

    To implement this event, simply use a drop-in replacement in our code sample.

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" & strComputer _

       & "rootcimv2")

     

    Set colEvents = objWMIService.ExecNotificationQuery _

       ("SELECT * FROM __InstanceModificationEvent 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 Modified:", _

           objEvent.TargetInstance.PartComponent

    Loop

    You can test this by again creating a folder called C:test.  Create a plain text file.  (It doesn’t matter what you name it.)  Now run your script.  While your script is running, open the text file in Notepad and add some text.  Now save your changes.  You script should notify you that the file has been changed.

    As you’ve probably noticed by now, WMI returns a pretty interesting file path.

    ComputerNamerootcimv2:CIM_DataFile.Name="c:scriptsDocument.txt"

    This is probably not what you want.  Let’s take a quick look at how to parse this into a usable file path.

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


       · Learn what other events are made available by WMI.
     

    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