Answered by:
Vbscript - cpu usage

Question
-
Hello
I need to obtain the cpu usage% (vbscript) like is show in task 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?Wednesday, November 4, 2009 5:49 PM
Answers
-
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 reveal percentage of cpu, but this might be the best you can do. I hope this helps.Richard Mueller
MVP ADSI- Marked as answer by RicardoMarques Thursday, November 5, 2009 3:07 PM
Wednesday, November 4, 2009 8:57 PM -
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, istrComputer = "."
intPasses = 5
intPause = 1000 'millisecondsSet objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colItems = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet
objRefresher.RefreshFor 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
MVP ADSI- Marked as answer by RicardoMarques Thursday, November 5, 2009 3:07 PM
Thursday, November 5, 2009 1:43 AM
All replies
-
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 reveal percentage of cpu, but this might be the best you can do. I hope this helps.Richard Mueller
MVP ADSI- Marked as answer by RicardoMarques Thursday, November 5, 2009 3:07 PM
Wednesday, November 4, 2009 8:57 PM -
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, istrComputer = "."
intPasses = 5
intPause = 1000 'millisecondsSet objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
Set colItems = objRefresher.AddEnum _
(objWMIService, "Win32_PerfFormattedData_PerfProc_Process").objectSet
objRefresher.RefreshFor 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
MVP ADSI- Marked as answer by RicardoMarques Thursday, November 5, 2009 3:07 PM
Thursday, November 5, 2009 1:43 AM -
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
ShashiWednesday, September 28, 2011 11:33 AM -
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.
Richard Mueller - MVP Directory Services- Proposed as answer by Arun Shourie Sunday, July 26, 2015 5:54 PM
Wednesday, September 28, 2011 2:55 PM -
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,
Shashi
Shashi
- Edited by Shashi.Surve Thursday, September 29, 2011 2:46 PM
Thursday, September 29, 2011 2:27 PM -
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
Monday, June 29, 2015 8:11 AM