WMI - Listing Processes
Listing Processes with Win32_Process
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 Win32_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.
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 Win32_Process 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
Script to List the Processes Running on the Computer
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try - it's free.
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:
3) Often, as in this instance, WMI requires security clearance in order to query the other machine's hardware, this is why we add :
4) Set colProcess = objWMIService.ExecQuery _ is a standard WMI phrase to prepare for the WQL command: Select * from Win32_Service
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.
SolarWinds' Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices. Boost your network performance by activating network device features you've already paid for.
Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its 'Traps' and 'Communities'. Try Config Generator now - it's free!
Simple PowerShell Equivelent - Get-Process
# PowerShell Get-Process list
Note 1: PowerShell's commands are not case sensitive, thus you could type Get-Process, or Get-Process. Also you can omit the 'get' in Get-Process, this is because 'get' is the default verb and PowerShell intelligently adds 'Get-' to process.
Note 2: Invariably, PowerShell uses singular nouns, thus Get-Process (and not Get-Processes).
Switches or modifiers for Get-Process
With Get-Process, the wildcard asterisk * is optional, however, it does remind us that we can modify the output to produce a restricted range:
Get-Process [ab]* returns all processes beginning with the letter a or b.
Get-Process [ae]* surprised me, it only listed process beginning with 'a', or beginning with 'e'. To get a range we must add a hyphen between the letters: Get-Process [a-e]*
If you have taken my advice and you have the Task Manager open, it's worth checking that what you see in PowerShell matches what you see in Task Manager.
Encouraging computers to sleep when they're not in use is a great idea - until you are away from your desk and need a file on that remote sleeping machine!
WOL also has business uses for example, rousing machines so that they can have update patches applied. My real reason for recommending you download this free tool is because it's so much fun sending those 'Magic Packets'. Give WOL a try - it's free.
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.