Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 3
    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


    Working with System Processes in WSH - Taking a look under the hood


    (Page 2 of 4 )

    Our first task is will be to list all currently running processes.  This is useful when making a system snapshot or just for curiosity's sake.  For this example we'll list each process by Name and Process ID.

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set colProcess = objWMIService.ExecQuery _

       ("Select * from Win32_Process")

     

    For Each objProcess In colProcess

       WScript.Echo objProcess.Name & ", PID: " & objProcess.ProcessId

    Next

    Here we're connecting to WMI and querying all objects belonging to the Win32_Process class.  This returns a collection of objects representing each of the currently running processes.  We are using the Name and ProcessID properties to return the Name and Process ID of each process respectively.

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set colProcess = objWMIService.ExecQuery _

       ("Select * from Win32_Process Where Name='explorer.exe'")

     

    For Each objProcess In colProcess

       WScript.Echo objProcess.Name & ", PID: " & objProcess.ProcessId

    Next

    A slight modification to our query allows this code to list specific processes instead.  In this above example, I've used a Where clause to limit the objects returned to only those with the name Explorer.exe.

    Again, this is useful if you are monitoring a specific process, however, we are kind of assuming that the process exists.  Another slight rewrite makes this code a little more effective.

    strProcess = "Explorer.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set colProcess = objWMIService.ExecQuery _

       ("Select * from Win32_Process Where Name = '" & strProcess & "'")

     

    If colProcess.Count < 0 Then

       WScript.Echo "Process is running."

    Else

       WScript.Echo "Process is not running."

    End If

    Instead of actually working with the objects, we're simply checking to see if the collection returned any objects.  This would be useful if you simply wanted to return a True or False value indicating whether a particular process was active in memory.

    A complete list of the properties and methods available through the Win32_Process can be found here.

    Now that we've seen how to check whether a process is running, it could be useful to see how to start and stop them accordingly.  Again, WMI provides a way for us to do this with very small modifications to our existing code sample.

    strProcess = "Explorer.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set colProcess = objWMIService.ExecQuery _

       ("Select * from Win32_Process Where Name = '" & strProcess & "'")

     

    For Each objProcess In colProcess

       objProcess.Terminate

    Next

    This code returns all instances of a specified process and then makes use of the Terminate method to end each instance.  Starting a process is done slightly differently.

    strProcess = "Explorer.exe"

    strComputer = "."

    Set objWMIService = GetObject("winmgmts: " _

       "{impersonationLevel=impersonate}!" & strComputer _

          & "rootcimv2")

    Set objProcess = objWMIService.Get("Win32_Process")

    Set objProgram = objProcess.Methods_("Create") _

       .InParameters.SpawnInstance_

    objProgram.CommandLine = strProcess

     

    Set objProcess = objWMIService.ExecMethod _

       ("Win32_Process", "Create", objProgram)

    Here we resort to the Create method.  This method is native to the Win32_Process class and is not provided by its objects as before.  This method creates a process instance in memory with the command line provided.  This command line should be a full path if this process is not located in the system path.

    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

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT