Vbscript to check CPU and Processor performance counter statistics using WMI
Posted by: Jerry Lees
By now, if your reading my blog you’ve had a chance to play with the script I posted for Using Vbscript to gather server performance counter data with WMI . But I think I might know what you’re thinking– “Hey, that’s cool… but I could careless about knowing how much non-paged pool memory there is being used on the system! I need to track something useful, like processor utilization or something else!
Well, you can– and here you go! As always the code itself is available on my website in the File Depot if you’d like to avoid copy/paste problems or you don’t want to type the code yourself.
This script has two functions in it, GetFormattedCPU() and GetRAWCPU() which both do the same thing, except that one uses a raw performance counter (Win32_PerfRawData_PerfOS_Processor) and the other uses a pre-formatted counter (Win32_PerfFormattedData_PerfOS_Processor) to get the information from the system. These two counter types, as you will see, give you very different numbers and for the most part you should probably stick with teh pre-formatted counters if at all possible so you don’t have to do a ton of math to get meaningful data.
The functions both return a Comma Delimited string that contains the CPU percantage usage for all processors in the system. Note that, on my dual core system I get 3 numbers– respective to the return value; Processor 0, Processor 1, and _Total. This should work equally well on Physical SMP systems that are either single or Dual Core, but I suspect on a single core system with more than two processors you will get two numbers that are identicle… unfortunately, I no longer have a single core system I can test on.
As always, the code is written in such a way that you can replace the “.” with a “server name” and it will work remotely on a system– providing you have administrative rights. Here is the code:
Option Explicit
On Error Goto 0
Dim strSrv, strQuery
strSrv = “.”
WScript.Echo GetFormattedCPU(StrSrv)
WScript.Echo”______”
WScript.Echo GetRAWCPU(StrSrv)
Function GetFormattedCPU(strSrv)
Dim objWMIService, Item, Proc
strQuery = “select * from Win32_PerfFormattedData_PerfOS_Processor”
Set objWMIService = GetObject(”winmgmts:\\” & StrSrv & “\root\cimv2″)
Set Item = objWMIService.ExecQuery(strQuery,,48)
WScript.Echo strQuery
For Each Proc In Item
GetFormattedCPU = GetFormattedCPU & Proc.PercentProcessorTime & “, “
wscript.echo “Processor ” & Proc.Name & ” = ” & Proc.PercentProcessorTime
Next
End Function
Function GetRAWCPU(StrSrv)
Dim objWMIService, Item, Proc
strQuery = “select * from Win32_PerfRawData_PerfOS_Processor”
Set objWMIService = GetObject(”winmgmts:\\” & StrSrv & “\root\cimv2″)
Set Item = objWMIService.ExecQuery(strQuery,,48)
WScript.Echo strQuery
For Each Proc In Item
GetRAWCPU= GetRAWCPU & Proc.PercentProcessorTime & “,”
wscript.echo “Processor ” & Proc.Name & ” = ” & Proc.PercentProcessorTime
Next
End Function
That’s it!! As a point of reference, the output of this script looks as follows:
select * from Win32_PerfFormattedData_PerfOS_Processor
Processor 0 = 0
Processor 1 = 0
Processor _Total = 0
0, 0, 0,
______
select * from Win32_PerfRawData_PerfOS_Processor
Processor 0 = 273670781250
Processor 1 = 275960625000
Processor _Total = 274815703125
273670781250,275960625000,274815703125,
Again, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems I’ve provided the code for download for you. From here on out I will make the code available for download from my website’s (www.webstemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!
Extra credit: Can you guess why I would have messed up your output from the functions in this script by separating the numbers with commas? I invite you to leave a comment and I’ll let you know if your on the same track as I am. Comments are always welcome!


