I wanted to tell you about another addition to the latest release of the PSScriptTools module. This is something I've written about before but I decided to add the function to the module. I hope you find it a much easier way to work with performance counters. And it works in Windows PowerShell and PowerShell 7.x.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Get-Counter
We've had the Get-Counter cmdlet in PowerShell for a very long time. However, I have always had a slight problem with the way it displays results.
The command writes an interesting object to the pipeline.
Under the hood, PowerShell is using a custom format file to render results in an easy to read view. But if you want to do something with the output like export to a csv file, you need to jump through hoops.
Get-Counter | Select-Object -expand Countersamples | Select-Object Timestamp,Path,InstanceName,CookedValue,@{Name="Computername";Expression={$_.path.split("\")[2].toUpper()}} | Export-Csv c:\work\counters.csv
This is certainly not intuitive. I wanted a performance counter cmdlet that wrote "better" structured data to the pipeline.
Get-MyCounter
The PSScriptTools module now includes an updated version of my Get-MyCounter function. This function is a "wrapper" around Get-Counter that writes simple, structured objects to the pipeline. You can use it in almost the same say as Get=Counter.
The custom object has its own formatting file. But the object type is easy to use.
Now, I have a much easier way to work with performance counter data.
In fact, I can get counters from Get-Counter and pipe them to Get-MyCounter.
Get-MyCounter also supports sampling.
get-counter -list system | get-mycounter -MaxSamples 10 -SampleInterval 2
Or you could use ConvertTo-WPFGrid, also from the PSScriptTools module, and let it handle the sampling.
$counters = (get-counter -list system).counter | where {$_ -match "file"}
Get-MyCounter -Counter $counters -ComputerName prospero,thinkp1 |
Select-Object * -exclude Instance |
ConvertTo-WPFGrid -Title "System Performance" -Timeout 6 -refresh -UseLocalVariable counters
The form will refresh every 6 seconds until I close it. The form also runs in its own runspace so there is no prompt blocking.
I hope you'll give Get-MyCounter a try and let me know what you think.
3 thoughts on “Better Performance Counters with PowerShell”
Comments are closed.