View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Michel Pierron Michel Pierron is offline
external usenet poster
 
Posts: 214
Default Task manager’s system CPU PercentProcessorTime

Hi Poniente;
Adapt something like:

Sub Test()
Const strComputer$ = "."
Dim objWMIService As Object, colProcess As Object, objItem As Object
Dim Ret$, strNameOfUser, strUserDomain, i%: i = 1
Cells.Clear
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * " _
& "from Win32_PerfFormattedData_PerfProc_Process", , 48)
Cells(1, 1) = "Process Name"
Cells(1, 2) = "CPU Usage"
Cells(1, 3) = "Process ID"
Cells(1, 4) = "User name"
Cells(1, 5) = "Domain"
For Each objItem In colProcess
If objItem.Name < "Idle" And objItem.Name < "_Total" Then
i = i + 1
Cells(i, 1) = objItem.Name
Cells(i, 2) = objItem.PercentProcessorTime
Cells(i, 3) = objItem.IDProcess
End If
Next
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objItem In colProcess
i = RowFind(ActiveSheet, 3, objItem.ProcessID, True)
If i Then
Ret = objItem.GetOwner(strNameOfUser, strUserDomain)
Cells(i, 4) = strNameOfUser
Cells(i, 5) = strUserDomain
End If
Next
Set colProcess = Nothing
Set objWMIService = Nothing
Columns("A:E").Columns.AutoFit
End Sub

Private Function RowFind(Sh As Worksheet, ByVal Col As Byte, What As
Variant, Whole As Boolean) As Long
Dim Word As Range, Who As Byte
If Whole Then Who = 1 Else Who = 2
Set Word = Sh.Columns(Col).Find(What, LookAt:=Who)
If Not Word Is Nothing Then RowFind = Word.Row
End Function

MP

"Poniente" a écrit dans le message de
...
Can anyone help me reproduce the values in the CPU column of the tab
‘processes’ of the Windows task manager in excel?
I’d like to integrate these values in the code below, which lists the
active processes.
Unfortunately, the line ‘objProcess.PercentProcessorTime’ does not
work.

Your help is appreciated!
Poniente

Sub OwnerOfProcesses()
Dim objWMIService As Object
Dim colProcessList As Object
Dim objProcess As Object
Dim strNameOfUser As Variant
Dim strUserDomain As Variant
Dim colProperties As String
Dim MyList() As Variant
Dim x As Long

Set objWMIService = GetObject(strWmgt)
Set colProcessList = objWMIService.ExecQuery(strWmiQ)

x = colProcessList.Count

ReDim MyList(0 To (x - 1), 0 To 4)
On Error Resume Next
x = 0
For Each objProcess In colProcessList
colProperties = objProcess.GetOwner(strNameOfUser, strUserDomain)
MyList(x, 0) = objProcess.Name
MyList(x, 1) = strUserDomain
MyList(x, 2) = strNameOfUser
MyList(x, 3) = objProcess.handle
MyList(x, 4) = objProcess.PercentProcessorTime ‘ Help requested:
this line generates an error
x = x + 1
Next
Range("Log_Processes").Resize(x, 5).Value = MyList
Set objWMIService = Nothing 'JBC
Set colProcessList = Nothing 'JBC
Set objProcess = Nothing 'JBC
End Sub