[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转贴] 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

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

TOP

返回列表