VBScript code: View CPU usage for a process name

On July 14, 2010, in How-to, Scripting, by Cubert aka (Cube Dweller)

So you need a VB script to check for a process or set of processes CPU usage? Let’s say I want to see all “svchost.exe” process PID’s and the amount of CPU each process is using? see in line 6 and 7 the name of the process your looking for? Just edit the name of the process and run the script. It will print out the PID and the CPU usage for each process it finds that matches the Process Name provided.

Then I would copy the code below to a .vbs file and run it in a the CLI.

DIM MyPID(20), objProcess, objItems, objitem
XX = -1
set objService = getobject("winmgmts:")
for each Process in objService.InstancesOf("Win32_process")
if Process.Name = "svchost.exe" Then XX = XX + 1
if Process.Name = "svchost.exe" Then MyPID(XX) = Process.processid
For I = 0 To 20
Set objProcess = GetObject("winmgmts:{impersonationLevel=impersonate}//localhost")
Set objItems = objProcess.ExecQuery("Select PercentProcessorTime from Win32_PerfFormattedData_PerfProc_Process where IDProcess=" &MyPID(I)& "")
for each objItem in objItems
Wscript.Echo MyPID(I) & " = " & objItem.PercentProcessorTime


Tagged with:

2 Responses to “VBScript code: View CPU usage for a process name”

  1. Jim says:

    Is this site active? Are you the author/owner of Alert On Failure? You list it on your supported sites, but most of your supported sites appear to be down.

    I found Alert On Failure on VMWare’s Appliance site, downloaded it and installed it. It ran and could ping the network from the root login, but I could not get any AOF config to ping (ICMP) anything and show any data for any nodes I set up.

    If you know anything about this, please reply to my email address.


  2. kiran says:

    how to find parent process id and its childerns id and cpu utilization of all children process(both in java and vbsript)

Leave a Reply


%d bloggers like this: