获取服务器的cpu利用率

时间:2015-03-28 21:47:09

标签: vbscript windows-server-2008-r2

我已经四处搜索并获得了显然适用于所有人的代码:

Set objWMIService2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
Set colItems = objWMIService2.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'") 
For Each objItem In colItems 
    Response.Write( "CPU Usage Percentage: " & objItem.PercentProcessorTime & "%" )
Next

执行它我得到一个完全空白的页面,而不是错误。

实际上os是在服务器上虚拟化的,这是问题吗?如果是这样,还有一个解决方法吗?

1 个答案:

答案 0 :(得分:1)

你得到的错误是什么?

这就是我正在使用同样的东西(剥离了一下)。它显示每物理处理器的负载。

strComputer = "."
Dim arrProcessors : ReDim arrProcessors(2,0)

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colCPUSystems = objWMIService.ExecQuery("Select AddressWidth,DataWidth,NumberOfCores,Name,MaxClockSpeed,LoadPercentage from Win32_Processor")
proc = 0
For Each objProc in colCPUSystems
    arrProcessors(0,proc) = objProc.NumberOfCores
    arrProcessors(1,proc) = objProc.MaxClockSpeed
    arrProcessors(2,proc) = objProc.LoadPercentage
    proc = proc + 1
    ReDim Preserve arrProcessors(2,proc)
    strOSBits = objProc.AddressWidth
    strHWBits = objProc.DataWidth
    strProcessorCores = objProc.NumberOfCores
    strProcessor = objProc.Name
    strProcessorSpeed = objProc.MaxClockSpeed
Next

For proc = 0 To UBound(arrProcessors,2)-1
    intLoad = arrProcessors(2,proc)
    intFree = 100-intLoad

    strProcessorInfo = strProcessorInfo & "Processor " & proc+1 _
    & ": " & arrProcessors(0,proc) & " Cores : Load " & intLoad & "%" & vbCrLf 
Next 

WScript.Echo strProcessorInfo