2009-06-05 7 views

Répondre

2

Il semble qu'il soit possible d'utiliser WMI conjointement avec des compteurs Perfmon. Veuillez vous référer à this article.

Voici le code correspondant:

For Each Process in GetObject("winmgmts:").ExecQuery("Select * from Win32_Process") 
    WScript.echo Process.name & " " & CPUUSage(Process.Handle) & " %" 
Next 


Function CPUUSage(ProcID) 
    On Error Resume Next 

    Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2") 

    For Each objInstance1 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'") 
     N1 = objInstance1.PercentProcessorTime 
     D1 = objInstance1.TimeStamp_Sys100NS 
     Exit For 
    Next 

    WScript.Sleep(2000) 

    For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'") 
     N2 = perf_instance2.PercentProcessorTime 
     D2 = perf_instance2.TimeStamp_Sys100NS 
     Exit For 
    Next 

    ' CounterType - PERF_100NSEC_TIMER_INV 
    ' Formula - (1- ((N2 - N1)/(D2 - D1))) x 100 
    Nd = (N2 - N1) 
    Dd = (D2-D1) 
    PercentProcessorTime = ((Nd/Dd)) * 100 

    CPUUSage = Round(PercentProcessorTime ,0) 
End Function 
+0

Merci pour le lien vers l'article. J'avais vu ça, et c'est ce que j'ai fini par utiliser. – Flynn81

Questions connexes