0

I've searched around and got this code that apparently works for everyone:

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

Executing it I get a completely blank page, not an error.

Actually the os is virtualized on the server, is this the problem? And if so, there's a workaround?

Fehu
  • 381
  • 1
  • 3
  • 18
  • Well if you got a blank page with no error that means that the code inside of `For Each` isn't being executed, which means `colItems` is empty. check your WMI query. Query something else for testing. – Azevedo Mar 30 '15 at 17:02

1 Answers1

1

What's the error you're getting?

Here's what I'm using for the same thing (stripped down a bit). It shows load per physical processor.

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
langstrom
  • 1,652
  • 11
  • 14
  • I got this: Processor 1: 2 Cores : Load % – Fehu Mar 30 '15 at 14:52
  • Is this only happening on a single machine? Looks like the performance counters may need to be rebuilt. Try running `lodctr /r` (from a command prompt) on the local machine. – langstrom Mar 30 '15 at 15:01
  • lodctr /r gives me this: "Error: Unable to rebuild performance counter setting from system backup store, error code is 2". It's a virtualized os on a remote server, it was autoconfigured from the provider but must be almost vanilla – Fehu Mar 30 '15 at 15:19
  • 1
    Check [this link](https://social.technet.microsoft.com/Forums/windows/en-US/9b01e1a6-d872-4f28-9280-f35d6ca02a9f/lodctr-r-error-code-2?forum=w7itprogeneral) - Make sure you're running as admin, if so try running from `c:\windows\syswow64` – langstrom Mar 30 '15 at 15:43
  • It rebuilds the settings from syswow64, but if I run the vbscript code still gives me a blank value for intLoad :/ – Fehu Mar 30 '15 at 15:50
  • Are you running the script itself as an admin? Admin on remote server, etc? – langstrom Mar 30 '15 at 17:19
  • As standard vbscript asp page – Fehu Mar 30 '15 at 19:02