标题: [转贴] 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
- 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
-
- 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
复制代码
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23258095.html
作者: Dolphin070 时间: 2015-1-13 15:42
这个得到的数据相当不准确啊
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |