Wednesday, October 3, 2012

let’s use Get-Counter in PowerShell.

Hi,

Perfmon is one the favourites tools of all IT admins. Every IT admin love this tool and we love Powershell too.

As a Powershell users most of my time is spent on powershell tool and sometime there is a need arrive when you want to check few performance counters. and as you know i am quite lazy is doing stuffs and I think that laziness is a  path to automation

I don’t want to to pent RunBox and then type PerfMon and then click on OK and then add the performance counter which i needed.

03-10-2012 12-28-21

In performance counters i don't care about Graphs , i do care about values.

03-10-2012 12-31-20

Powershell give us a very nice and clean cmdlet to get the values of Performance counters. As i mentioned above I don’t care about the graph i do care about values and in powershell console every thing is Text and we love that texts.

To get the values of performance counter we need to use the cmdlet Get-Counter .

Lets Start.

To list all the performance counters sets use the command.

Get-Counter -ListSet *

 

This will show you the list and path of all Counter sets.

03-10-2012 12-35-29 

If you want to know what kind of Counter sets you do have in your system just piped the above command to the Select CounterSetName cmdlet

Get-Counter -ListSet * | select CounterSetName

03-10-2012 12-39-03

we can also use wildcards in -ListSet parameter. I am looking for all disk related counter sets, to search all conter sets  those have disk word in it i can use *Disk* argument in -ListSet parameter

Get-Counter -ListSet *disk*

03-10-2012 12-39-55

I am interested in counter set Logical Disk. and i can piped the above command to show me the LogicalDisk counter set only using where-Object cmdlet.

Get-Counter -ListSet *disk* | where {$_.CounterSetName -eq "LogicalDisk" }

03-10-2012 12-47-02

So now there is a little problem arise. if you want to see the detail list of counters and Paths, some how you can’t see the complete output. The whole output is truncated using … ,

03-10-2012 12-44-55

We need a list of counter to use them ,the best workaround of the above problem is to save the above command in to a variable.

$DiskCounter =  Get-Counter -ListSet *disk* | where {$_.CounterSetName -eq "LogicalDisk" }

we are saving the output of above command in to a $DiskCounter variable.

03-10-2012 12-51-37

So now if you want to see the Logical Disk Performance counter paths we can use Dot Notation now.

$DiskCounter.Paths

03-10-2012 12-54-35

if you want see the Logical Disk Counters only.

 $DiskCounter.Counter

03-10-2012 12-55-15

Of if you want to see the Logical Disk Performance Counter paths with instances with counters use.

$DiskCounter..PathsWithInstances

03-10-2012 12-56-22 

Okies Now.

I am interested in my C drive Disk Queue Length. and using $DiskCounter.PathsWithInstances i know that i have to use "\LogicalDisk(C:)\Avg. Disk Queue Length" counter.

Get-Counter -Counter "\LogicalDisk(C:)\Avg. Disk Queue Length" -SampleInterval 10

In above command we are using counter "\LogicalDisk(C:)\Avg. Disk Queue Length" to monitor the disk queue length of our C drive and using -SampleInterval  parameter we are telling to get-Counter cmdlet to collect the data for 10 seconds.

03-10-2012 13-02-37

if you want to see or monitor the counters you can use the -Continuous parameter.

Get-Counter -Counter "\LogicalDisk(C:)\Avg. Disk Queue Length" -Continuous

03-10-2012 13-05-04

That’s all for now ,, and i think now have an idea how to use it Smile 

Thanks

Aman Dhally

join aman on facebook Join aman on Linkedin follow aman on Twitter

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.