SunQuest
 
       Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Working with System Processes in WSH
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 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Working with System Processes in WSH
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-01-15

    Table of Contents:
  • Working with System Processes in WSH
  • Taking a look under the hood
  • More fun with processes
  • Modifying processes

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Working with System Processes in WSH - More fun with processes


    (Page 3 of 4 )

    To this point, everything we've done has been in a snapshot state.  By this I mean that we have only been able to work with the processes that are running at the moment our script is executed.

    What if we want a script to monitor processes and notify us whenever one starts or stops?  To do this, we can use the Win32_Process class with a bit of event-based programming.

    strProcess = "iexplore.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set objEventSrc = objWMIService.ExecNotificationQuery( _

       "SELECT * FROM __InstanceCreationEvent " & _

           "WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'" _

             & "And TargetInstance.Name = '" & strProcess & "'")

      

    Do While True

       Set objEvent = objEventSrc.NextEvent()

       WScript.Echo strProcess & " has been started."

       Exit Do

    Loop

    Event scripting is slightly beyond the scope of this article.  Understand that WMI is able to monitor certain events.  In this example we are monitoring the __InstanceCreationEvent which tells WMI whenever a new object is created in memory.  We then use a notification query to check whether that particular event was our process starting.

    Notice how we've used an infinite loop to ensure that our script doesn't stop running.  Thus it will continue to monitor each new event until Internet Explorer is started.

    strProcess = "iexplore.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set objEventSrc = objWMIService.ExecNotificationQuery( _

       "SELECT * FROM __InstanceDeletionEvent " & _

           "WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'" _

              & "And TargetInstance.Name = '" & strProcess & "'")

      

    Do While True

       Set objEvent = objEventSrc.NextEvent

       WScript.Echo strProcess & " has been stopped."

       Exit Do

    Loop

    Waiting for a process to end is exactly the same except that we will be watching the __InstanceDeletionEvent to know when a process is unloaded from memory instead.

    Again, the nature of how this code works is a bit beyond the scope of this article.  Just understand that this is the basic query structure you will need to use.  You can modify this query slightly to achieve other effects as well so don't be afraid to play with it.  For more information, you can check out my articles on event scripting with WMI.

    Event Scripting with WMI

    More Event Scripting with WMI

    More Windows Scripting Articles
    More By Nilpo


       · Learn how to capture the power of WMI in your WSH scripts to manipulate, create, and...
     

    WINDOWS SCRIPTING ARTICLES

    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions
    - VBScript: Functioning with Strings
    - Working with the Windows Registry in C++
    - Understanding Objects
    - HTML Applications: Giving WSH a User Interfa...
    - Modifying Computer Objects with Active Direc...
    - Logon Script to Send Email Notifications
    - Securing Computers and Active Directory
    - Moving and Renaming Computers with Active Di...
    - Working with System Processes in WSH





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