Board logo

标题: [转贴] VBS脚本获取进程的CPU使用率 [打印本页]

作者: forfiles    时间: 2012-8-29 16:34     标题: VBS脚本获取进程的CPU使用率

I have a need to capture the percentage of CPU usage for any processes that are using the CPU on an assortment of windows 2000 boxes and I want to check every minute.  I am using vbscript and WMI to capture a few other things such as memory usage and total CPU usage at the same time with the same script.  I have found the example script below but it takes 80-90 seconds on average to complete and I want to capture the data every minute.  Is there any way to speed this process up by perhaps checking every process at once?  I have this working with Windows 2003 but it is using the cooked counters available there that are not available on 2000.  If it helps I don't care about any sub 1% processes as these inconsequential and will dropped from my final data.

Thanks

  1. for each Process in GetObject("winmgmts:").ExecQuery("Select * from Win32_Process")
  2.     WScript.echo Process.name & " " & CPUUSage(Process.Handle) & " %"
  3. Next
  4. Function CPUUSage( ProcID )
  5.     On Error Resume Next
  6.     Set objService = GetObject("Winmgmts:{impersonationlevel=impersonate}!\Root\Cimv2")
  7.     For Each objInstance1 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
  8.         N1 = objInstance1.PercentProcessorTime
  9.         D1 = objInstance1.TimeStamp_Sys100NS
  10.         Exit For
  11.     Next
  12.     For Each perf_instance2 in objService.ExecQuery("Select * from Win32_PerfRawData_PerfProc_Process where IDProcess = '" & ProcID & "'")
  13.         N2 = perf_instance2.PercentProcessorTime
  14.         D2 = perf_instance2.TimeStamp_Sys100NS
  15.         Exit For
  16.     Next
  17.     ' CounterType - PERF_100NSEC_TIMER_INV
  18.     ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
  19.     Nd = (N2 - N1)
  20.     Dd = (D2-D1)
  21.     PercentProcessorTime = ( (Nd/Dd))  * 100
  22.     CPUUSage = Round(PercentProcessorTime ,0)
  23. End Function
复制代码


http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23258095.html
作者: Dolphin070    时间: 2015-1-13 15:42

这个得到的数据相当不准确啊




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2