Performance Counters? There’s a script for that.

Performance Counters? There’s a script for that.

Rate This
  • Comments 9

It’s not hard to get Performance Monitor counters in PowerShell 1.0, and it’s REALLY easy in 2.0. This is one I used yesterday to monitor network bytes in and out on 2.0 (keep all of the green lines in this post on the same line) :

get-counter -Counter "\Network Interface(*)\Bytes Total/sec" -SampleInterval 1 -MaxSamples 10

 

The –SampleInterval is in seconds, and the –MaxSamples sets how many you want. The –Counter parameter is the object and counter you want to monitor. Don’t know what they are for your system? No problem:

get-counter -ListSet *

 

Need to hit another computer? Easy Peasy – doesn’t even require remoting to be turned on (but you do need to have the rights to hit the perf counters):

get-counter -ComputerName COLLOSSUS -Counter "\Network Interface(*)\Bytes Total/sec" -SampleInterval 1 -MaxSamples 10

 

If your machine happens not to be named “COLLOSSUS”, just change that part J. And yes, you can use whatever counters you like, including SQL Server locks, blocks, etc. The full reference for get-counter is here: http://technet.microsoft.com/en-us/library/dd367892.aspx

 

Script Disclaimer, for people who need to be told this sort of thing:

Never trust any script, including those that you find here, until you understand exactly what it does and how it will act on your systems. Always check the script on a test system or Virtual Machine, not a production system. All scripts on this site are performed by a professional stunt driver on a closed course. Your mileage may vary. Void where prohibited. Offer good for a limited time only. Keep out of reach of small children. Do not operate heavy machinery while using this script. If you experience blurry vision, indigestion or heartburn during the operation of this script, see a physician.

Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post
  • I believe that you need the remote registry service to be enabled on Windows 2008 & beyond to collect performance counters remotely.

  • Correct - if you can't check the counters using the graphical tools, this won't work either. They make the same call.

    Thanks for reading!

  • Thank you for the Powershell tips. Not being an OOP person makes it difficult at times to wrap my head around it.

    May I please,please use your disclaimer!

  • Glad I could help!  And feel free to use the disclaimer...

  • Hi Buck,

    Continuing to enjoy the blog very much. Quick follow up question.  I've searched for this particular pain point several times and can't seem to find the single version of the truth.  Often, as a Sql DBA, I need to ascertain the port # on which Sql is running on a particular server.  How can I accomplish that from my desktop to any of my Sql boxes in my enterprise?  I know it's there somewhere, as we have access to the SMO, but where oh wise one?

    Wondering,

    Thanks.

  • Interestingly, you don't need to ask SQL Server at all. Just use the registry: http://www.databasejournal.com/features/mssql/article.php/3764516/Discover-SQL-Server-TCP-Port.htm

  • Fantastic, thanks!  

  • So the reurn from this is more than just the counter value. How would I extract the actual counter value from the returned "\\nycitdl8333\network interface(intel[r] wireless wifi link 4965agn _2)\bytes total/sec :9182"

    Being that the value can be any number of digits getting only the last # of chars doesnt work.

  • Thanks for the code-ee bits.  LOVE your disclaimer

Page 1 of 1 (9 items)