WMI Scripts

 

WMI - Listing Processes

Introduction to Listing Processes with WMI

This page is the foundation for a trilogy of Process pages. The sequence is: list, stop and finally start (restart) the process.

Even if your mission is to start or stop a process, the logical place to begin is with listing the processes running on a computer. The benefit of beginning with this page, which just lists the processes, is that you can learn the WMI methods without the risk of killing the wrong program.

The benefit of beginning with just listing the processes is that you can learn the WMI methods without the risk of killing the wrong program.

Topics for Listing and Terminating a Process

Scenario - Why you would want to List a Process?

Before creating a script, which will stop the process, you need to research the precise name of the corresponding program.  One way to investigate the names would be to Launch Task Manager, select the Application tab, right click the program and then choose, Go to Process.   An example of this link is Applications shows  Microsoft FrontPage and the corresponding process is frontpg.exe

Incidentally, the screen shot on the right reminds us that VBScript automates steps that you could walk through manually.  In this instance, we could click on the Processes tab to view all the running programs.

Example 1 - List the Processes Running on the Computer

Whilst our goal is to control programs on another machine by using a VBscript, let us begin with a script which simply lists the processes.  Then, if necessary, we could append code to the script and thus terminate one of those processes.  The key part of both scripts is where they connect to the CIM class called Win32_Process.  Each script begins with GetObject winmgmts and ExecQuery commands.  The second example adds a terminate  process command.

Prerequisites for your WMI Script

No specific requirements.  I cannot think of an operating system that does not have the Win32_ComputerSystem Class.

Instructions for  Listing Processes WMI Script

  1. Copy and paste the example script below into notepad or a VBScript editor.
  2. Decide which machine on your network to interrogate and then change line 10:
    My default value of, strComputer = "." means use the current machine.
  3. Save the file with a .vbs extension, for example: Process.vbs 
  4. Double click Process.vbs and check the list of processes.

Script to List the Processes Running on the Computer

 

' Process.vbs
' Free Sample VBScript to discover which processes are running
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.4 - December 2005
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strList

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")

For Each objProcess in colProcess
strList = strList & vbCr & _
objProcess.Name
Next

WSCript.Echo strList
WScript.Quit

' End of List Process Example VBScript

'

WMI Tutorial - Learning Points

From a WMI perspective

1) If you are experienced with WMI, then the two features to concentrate on are, Win32_Service and objProcess.name.

2)  If you are new to WMI then you will soon appreciate that all WMI scripts begin by employing winmgmts to access the root of the CIM library, here is the command:
Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")

3) Often, as in this instance, WMI requires security clearance in order to query the other machine's hardware, this is why we add :
 & "{impersonationLevel=impersonate}!\\" _  

4) Set colProcess = objWMIService.ExecQuery _ is a standard WMI phrase to prepare for the WQL command:  Select * from Win32_Service
".  The part we are particularly interested in is _Process.  Win32 has dozens of properties, here we need to query the Process component and not the Service or PhysicalDisk.

From a VBScript perspective

5) What makes scripting so powerful is the speed with which VBScript loops through an array of objects or properties, in this instance the loop is controlled by: For Each....In... Next.

6)  I am particularly proud of the other loop, in scripting terms it's primitive almost a non-entity, but to me it makes the output easier to read, strList = strList & vbCr & objProcess.name.

7) The only property of objProcess that we are interested in is, .Name.  However, for other examples we could substitute different properties, for example .ProcessId or .PeakVirtualSize.

8) It is also possible to output the process information not to the screen but to a file.  VBScript has all the tools you need to create a file and write a service on each line.

Summary of List Process

This page provides the foundation skills necessary to control Windows processes.  Before you start or stop a process, you need a script which just lists the processes running.  This page features a simple script, which lists the processes (programs) and then echoes the result to the screen.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


See Also

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.