Accessing Performance Counters in PowerShell

Originally published June 28, 2007

 

It's just so darn easy!

I needed to monitor something going on, on one of my servers. It was just a temporary need, so I didn't want to install a monitoring package (such as Servers Alive). What I needed to see is exposed as a performance counter (perfmon object, whatever you want to call it), so I figured I'd spend a few minutes seeing if I could do it in PowerShell.

Well, lo and behold, it's easy. The Microsoft MSDN page that talks about the .Net object is here.

First, you create a reference to the Performance counter:

$perf = New-Object System.Diagnostics.PerformanceCounter("MODUSADM", "Processing Queue Length", "", "zippy")

where the first parameter is the Performance Object, the second is the specific counter within the object, the third is the “instance“ of the counter, and the fourth is the name of the computer.

Note: this is a read-only reference. It's possible to get read-write references (see the Microsoft page).

Once you have the object in $perf, you simply access the data as you would any other variable:

while (1)
{
        $perf.RawValue
        Start-Sleep -seconds 1
}

and then ctrl-C it when you don't want the value displayed anymore.

Now that's easy!

It opens up loads of opportunities for new scripts...

Published Tuesday, November 13, 2007 8:51 PM by michael
Filed under:

Comments

Monday, February 09, 2009 12:09 PM by Get-Counter in Powershell V2 CTP3 | keyongtech

# Get-Counter in Powershell V2 CTP3 | keyongtech

Pingback from  Get-Counter in Powershell V2 CTP3 | keyongtech