Vbscript - cpu usage
Hello

I need to obtain the cpu usage% (vbscript)like is show intask manager.
I have this code:
For Each objItem in colProcess
WScript.Echo objItem.Name & ","& objItem.PercentProcessorTime
Next

This show process name but not cpu usage give another value that is not in task manager.

Any solution for this?
November 4th, 2009 5:49pm

Per "Microsoft Windows 2000 Scripting Guide", this script displays processor use by a process:
======
' Specify the local computer.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process")
For Each objProcess In colProcesses
sngProcessTime = (CSng(objProcess.KernelModeTime) _
+ CSng(objProcess.UserModeTime)) / 10000000
Wscript.Echo objProcess.Name & ", " & sngProcessTime
Next
=========
You can add a WHERE clause to the WMI query to restrict the output to one specified process. This does not revealpercentage of cpu, but this might be the best you can do. I hope this helps.

Richard Mueller

Free Windows Admin Tool Kit Click here and download it now
November 4th, 2009 8:57pm

A script I found in the Technet Script Center Gallery to output cpu usage of all processes:
========
Option Explicit
Dim strComputer, intPasses, intPause, objWMIService
Dim objRefresher, colItems, objItem, i

strComputer = "."
intPasses = 5
intPause = 1000 'milliseconds

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colItems = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet
objRefresher.Refresh

For i = 1 to intPasses
Wscript.Sleep intPause
Wscript.Echo vbCrLf & "Pass " & i
Wscript.Echo Now
For Each objItem in colItems
objRefresher.Refresh
Wscript.Echo vbCrLf & "Name: " & objItem.Name
Wscript.Echo " Creating Process ID: " & objItem.CreatingProcessID
Wscript.Echo " Percent Processor Time: " & _
objItem.PercentProcessorTime
Next
Next
========
Again, you can add a WHERE clause to the WMI query (or an If statement in the For Each loop)to restrict the output to one process.

Richard Mueller

November 5th, 2009 1:43am

Hi Richard,

 

VB Script which you copied form technet is not applicable for Windows 2000.

If you can provide the script for windows 2000 which can list down the processes with %CPU utilization exculting IDLE and SYSTEM will be appriciated.

 

Regards,

Shashi

Free Windows Admin Tool Kit Click here and download it now
September 28th, 2011 11:33am

The Win32_Process class is supported on Windows 2000 and above. The Win32_PerfFormattedData_PerfProc_Process class is only supported on Winodws XP or Windows Server 2003 and above. See this link:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394277(v=vs.85).aspx

I don't know of any alternatives available to a script.

 

September 28th, 2011 2:55pm

Hi Richard,

Thanks for your reply but the script you provided one with Win32_Process class does not provide CPU % usage per process.

I am aware that the Win32_PerfFormattedData_PerfProc_Process class is only supported on Windows XP or Windows Server 2003 and above. I need the script for Windows 2000 which will give me the snapshot of the processes with CPUUsage in % as we can view in Task manager .

 

Regards,

Free Windows Admin Tool Kit Click here and download it now
September 29th, 2011 2:27pm

Hi Shashi

I am struck at the same place you were struck years ago.

I need to output Percentage of CPU utilization by each process as is available in the task manager in Windows 2000 servers.

Thanks

Arun Shourie

June 29th, 2015 4:12am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics