PDA

View Full Version : Solved: Running Process (Processor %/Memory Usage)



Ebrow
12-03-2007, 03:14 PM
Hi,

I am trying to obtain all running system processes (which are shown in the the task manager). and need the following information to be updated every set period of time (hopefully 1 second or faster i.e 1/10th sec)

The data I require are:

Process CPU %
Process Mem Usage
Process Image Name
Process User Name

I have started with the attached spreadsheet, which uses WMI to enquire the process name and process cpu %. When I run this code, the CPU % does not reflect the correct CPU% it does not seem to capture correctly. Only the WMI service moves from 0 to 100 randomly and thats all. :banghead:

7440

Am I doing something wrong? Can someone help me understand WMI a little better. Or is there a better way to get this information (API Hook or something?).



Sub Testing()
strComputer = "."
Cells(1, 1).Value = "Process Name"
Cells(1, 2).Value = "CPU Usage"


Do Until x > 1
myRow = 1

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfProc_Process", , 48)

For Each objItem In colProcesses
If objItem.Name <> "Idle" And objItem.Name <> "_Total" Then
myRow = myRow + 1
Cells(myRow, 1).Value = objItem.Name
Cells(myRow, 2).Value = objItem.PercentProcessorTime
End If
Next

DoEvents
Loop
End Sub

Marcster
12-04-2007, 12:16 PM
Have a look at the Win32_Process class.

Name = Name of the executable process
ExecutablePath = Path to the executable of the process
WorkingSetSize = Memory amount (Bytes) used for the process

Marcster.

Ebrow
12-04-2007, 12:41 PM
Thanks,

I got those. Do you know what to use for CPU%. I can't get PercentProcessorTime to give true CPU% (figures differ to what is on Task Manager)

Marcster
12-04-2007, 12:58 PM
Maybe try refreshing:

http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0421.mspx

Marcster.

Ebrow
12-04-2007, 01:50 PM
Woo Hoo! The refreshing did it. Thanks very much!!!

Great artical

Ebrow
12-04-2007, 01:59 PM
This link can be useful to:-

WMI Code Creator (VBScript / VB.Net), could be improved upon, but good for a start.

http://www.microsoft.com/downloads/details.aspx?FamilyID=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en