Board logo

标题: [原创] VBScript监测指定进程的CPU占用率 [打印本页]

作者: IUnknown    时间: 2012-9-1 16:33     标题: VBScript监测指定进程的CPU占用率

  1. ProcessName = "ttplayer.exe"
  2. Set objSWbemService = GetObject("winmgmts:\\.\root\cimv2")
  3. set colItems = objSWbemService.ExecQuery("select * from Win32_Processor")
  4. for each objItem in colItems
  5.     CpuNum = CpuNum + objItem.NumberOfLogicalProcessors
  6. next
  7. set colItems = objSWbemService.ExecQuery("select * from Win32_Process " _
  8.     & " where Name = '" & ProcessName & "'")
  9. for each objItem in colItems
  10.     ProcessId = objItem.ProcessId
  11.     exit for
  12. next
  13. set colItems = objSWbemService.ExecQuery( _
  14.     "select * from Win32_PerfFormattedData_PerfProc_Process " _
  15.     & " where IDProcess = " & ProcessId)
  16. for each objItem in colItems
  17.     'WScript.Echo objItem.Path_.RelPath
  18.     set SWbemObject = objItem
  19. next
  20. SWbemObject.Refresh_
  21. do
  22.     SWbemObject.Refresh_
  23.     WScript.StdOut.write vbCr & Time() & vbTab _
  24.         & (SWbemObject.PercentProcessorTime / CpuNum) & "%" & vbTab
  25.     WScript.Sleep 1000
  26. loop
复制代码
一个程序可能有多个进程实例,这里只处理单进程的情况。要处理多个进程也很简单,有需要者请自行修改。




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