Those that have read some of my prior posts on Windows Server 2012 would note that I like to write about new features and capabilities.  Windows Server 2012 and Windows 8 both provide PowerShell cmdlets allowing you the ability to gather and output performance data.  The geek in me is all over this with ideas, especially when it comes to managing multiple systems in my basement lab.  Using great new performance data cmdlets and a few PowerShell techniques, the things you could resolve!  Or…at least, just imagine the data you could collect!   Let’s start with a few of these cmdlets that are new to Windows 8 and Windows Server 2012.

Important New PowerShell Cmdlets for Performance Data Gathering

Get-Counter

It became obvious fairly quickly that one of the most important cmdlets to use for obtaining performance data with PowerShell is Get-Counter.   As shown in the example below, you can specify an object\counter path to obtain very specific data.

 

Figure 1: Get-Counter -Counter "\Processor(_Total)\% Processor Time"

 

The above example shows how to obtain an instantaneous reading of a particular counter.  However, when approaching a performance issue, typically one should look at trends or sustained activity.  We will build up to that.  As you know, PowerShell cmdlets typically return a collection of objects with elements, methods, and properties.  Therefore, to see what actually gets returned from this sample query, you could pipe the results to Get-Member to see what really gets returned.  This is a perfect example of how to snoop and see what other elements you could leverage.

 

Figure 2: Get-Counter -Counter "\Processor(_Total)\% Processor Time" | Get-Member

 

Like with most cmdlets in PowerShell, you can’t unleash their full potential without understanding all the options and what gets returned.  The timestamp property if valuable for interpreting data, but so are the directives to the cmdlet about how you want it to obtain data.  More specifically, do you want it done at interval or just instantaneously?  The options –SampleInterval and –MaxSamples may be used to control sampling behavior.  To take this further, the syntax for Get-Counter allows you to specify a string of computer names to retrieve data from using –ComputerName “string of computer names.

Export-Counter

While you could use Get-Counter to gather data and then act immediately upon it with PowerShell, you might also have the occasion to export collected data.  Export-Counter allows you to output counter data in a variety of formats. Perhaps you’re planning to import the data into Excel and want a delimited format like CSV.  Maybe you’d simply like to look at the data with Performance Monitor, so BLG format would be your choice.  Use the –FileFormat “format option to specify the format.  You could also make a circular BLG file with a size limit.  Use –MaxSize to limit the size of the BLG file, -Circular to specify this file will be a circular buffer of log data.   The –Force option will overwrite an existing data file if one already exists.  Note: Export-Counter must be passed a collected counter set similar to the Get-Counter example noted above.

 

Figure 3: PS C:\Users\mlucas> Get-Counter -Counter "\Processor(_Total)\% Processor Time“ | Export-counter -Path $home\PercentProcessorTime.blg -Force -FileFormat "BLG"

Import-Counter

Say, for instance, that you’ve already collected some performance data but you’re going to brew your own script to analyze performance data.  Import-Counter is an ideal cmdlet for bringing data back into PowerShell so that something can be done with it.  To import counters from a file with at least a few data samples into a variable called $Counters use a command similar to: 

$Counters = Import-Counter -Path $home\PercentProcessorTime.blg

What I like about this cmdlet is the ability to specify more than one file for import.  The syntax for specifying multiple files is a little different than you might think, but it works:

$Counters = “c:\test\log1.blg”, “c:\test\log2.blg” | Import-Counter

Practical Usage Scenario

Recall that when you launch Performance Monitor, the default counter shown in a live view is of %Processor Time (_Total) for all processors.   A good foundation to build upon is the equivalent perspective from PowerShell of charting the same kind of data.

For this example, we want to obtain an instantaneous reading of the %Processor Time that is a total of all processors.  From the first example above, you notice the output column showing % Processor Time reads “Counter Samples”.  To look at the actual double precision value itself, we look at CounterSamples.CookedValue. 

$pt = (Get-Counter -Counter "\Processor(_Total)\% Processor Time").CounterSamples.CookedValue

$pt

1.92418675013658

In this example, the return value is nearly two percent.  Therefore, at the given instant the counter was gathered, overall processor was just under two percent which suggests low processor utilization.   You could leverage powershell cmdlets to format this data or otherwise round the value.   However, if you define the $pt variable used in this example as an integer, PowerShell will round the number for you.  Therefore, the rounded number would be 2 percent.  Let’s try the same example, but casting $pt as an integer.

[int]$pt = (Get-Counter -Counter "\Processor(_Total)\% Processor Time").CounterSamples.CookedValue

$pt

2

That’s a great start.  However, not yet practical.  That’s only one data point.  Using the –SampleInterval option for Get-Counter, PowerShell can do most of the work for us without having to setup a loop or array.

 

Figure 4: $pt = (Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 10).CounterSamples.CookedValue

The above example achieves a list of samples of the % Processor Time counter, but only the values.  What we really want next is to be able to receive all the other object data as well including timestamps.  We can simply drop .CounterSamples.CookedValue to be a little less specific.

 

Figure 5: $pt = (Get-Counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 10)

Quite simply, with collected data you can export data from the above example using Export-Counter.  In fact, you can easily view it using Performance Monitor.

 

Figure 6: $pt | Export-counter -Path $home\PercentProcessorTime.blg -Force -FileFormat "BLG"

.\PercentProcessorTime.blg

 

Figure 7: Viewing with Performance Monitor

 

You could easily change the sample interval to adjust how often the cmdlet gathers data.  The number of samples can easily be adjusted as well.   Now, you may be thinking that this example isn’t so practical because we’ve gathered only one counter.  The good news is that Get-Counter accepts multiple counters as a delimited list…so you can gather much more.  Here is another version of the same technique that gathers two counters and 100 data points.

 

 

Figure 8: Supplying a list of counters

 

Figure 9: Results

Concluding Thoughts

PowerShell is a definite option for gathering, analyzing, or exporting performance data.  The key is to know what to specify on the Get-Counter cmdlet to obtain what you’re looking for.   Hopefully these examples are enough to get you started.

For More Information

Get-Counter

http://technet.microsoft.com/en-us/library/hh849685.aspx

Export-Counter

http://technet.microsoft.com/en-us/library/hh849683.aspx

Import-Counter

http://technet.microsoft.com/en-us/library/hh849686.aspx

 

For more details about other PowerShell cmdlets for Windows 8 and Windows Server 2012:

http://technet.microsoft.com/en-us/library/dn264983.aspx

 

Until next time!

Martin