VBScript监测指定进程的CPU占用率

标签: , , , , ,

VBScript监测指定进程的CPU占用率,一个程序可能有多个进程实例,这里只处理单进程的情况。要处理多个进程也很简单,有需要者请自行修改。

ProcessName = "ttplayer.exe"

Set objSWbemService = GetObject("winmgmts:\\.\root\cimv2")

set colItems = objSWbemService.ExecQuery("select * from Win32_Processor")
for each objItem in colItems
    CpuNum = CpuNum + objItem.NumberOfLogicalProcessors
next

set colItems = objSWbemService.ExecQuery("select * from Win32_Process " _
    & " where Name = '" & ProcessName & "'")
for each objItem in colItems
    ProcessId = objItem.ProcessId
    exit for
next

set colItems = objSWbemService.ExecQuery( _
    "select * from Win32_PerfFormattedData_PerfProc_Process " _
    & " where IDProcess = " & ProcessId)
for each objItem in colItems
    'WScript.Echo objItem.Path_.RelPath
    set SWbemObject = objItem
next

SWbemObject.Refresh_
do
    SWbemObject.Refresh_
    WScript.StdOut.write vbCr & Time() & vbTab _
        & (SWbemObject.PercentProcessorTime / CpuNum) & "%" & vbTab
    WScript.Sleep 1000
loop

原文链接:http://bbs.bathome.net/viewthread.php?tid=18807

赞赏

微信赞赏支付宝赞赏

随机文章:

  1. /DYNAMICBASE链接选项
  2. VBS中Property Set和Property Let的区别
  3. XPM BMP格式转换工具
  4. 一个复杂的VBS脚本问题——变量的变量
  5. Windows 7在桌面创建宽带连接快捷方式

留下回复