Accessing PerfMon counters from Powershell

These lines of code can be used for all sorts of purposes where Performance Monitor counters are needed in a Powershell script or just as a one time deal on the command line.

I’ve been using the example below to get “snapshots” of how a Jetstress test is going. Jetstress tests, as you know, can run for an extensive period of time before showing the final result, so it’s nice to get an indication of the result beforehand (without having to launch perfmon and adding counters etc.).

$jetstresscounters=@()

"MBX01", "MBX02", "MBX03" | % {
$jetstresscounters+= "\\$_\MSExchange Database(JetstressWin)\I/O Database Reads Average Latency"
$jetstresscounters+= "\\$_\MSExchange Database(JetstressWin)\I/O Log Writes Average Latency"
}
Get-Counter -Counter $jetstresscounters -ErrorAction:SilentlyContinue -MaxSamples 1 -SampleInterval 1

The example will read one sample of each counter from each of the servers (can be executed remote) and present the result when all counters have been read.

Increase MaxSamples to get more samples for each counter and SampleInterval to have the samples further apart. Notice that this of course will increase the run time of the script.

Hope you will find it useful in your deployment and testing.

Thanks to Bo Eschricht for idea and creative coding… Smile