Posts
49
Comments
83
Trackbacks
0
PowerShell snippet to get perfcounters
Somebody at work asked me whether its possible to get perfcounters through PowerShell. The answer, as with any question directed at this blog, is yes. Here is a little snippet below to do just that. Hint: its possible to compress this to a single line, experiment with passing in arguments to new-object it if you don't want to create a script like the one below:   
param(
$CounterName=$(read-host CounterName),
$CategoryName=$(read-host CategoryName),
$InstanceName=$(read-host InstanceName),
$MachineName="."
) 
## Example CounterName = "% Processor Time"
##         CategoryName = "Processor"
##         "_Total"
##
##   get-perfcounter "Disk Transfers/sec" "PhysicalDisk" "_Total" 

$counter = new-object System.Diagnostics.PerformanceCounter
$counter.CounterName = $CounterName
$counter.CategoryName = $CategoryName
$counter.InstanceName = $InstanceName
$counter.MachineName = $machinename

$counter
posted on Monday, July 17, 2006 3:07 PM Print
Comments
Gravatar
# 
Lee
7/17/2006 7:07 PM
  
I also wrote a poll-process script that uses performance counters to emulate "top": http://www.leeholmes.com/blog/AccessingPerformanceCountersInMSH.aspx

Lee
Gravatar
# 
Jeffrey Snover
7/18/2006 12:07 PM
  
PSMDTAG:FAQ: How do I access performance counters?

PSMDTAG:DOTNET PerformanceCounter

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 2 and 7 and type the answer here:
News
A little slow these days as I'm busy working on exchangelabs.com. I will try and post tidbits when I get some time. Enjoy the older posts till then!