Summary: Microsoft Scripting Guy, Ed Wilson demonstrates how to use the Out-GridView cmdlet with performance counter information.
Hey, Scripting Guy! I have a question, and hopefully you will not think it is a stupid one. During the 2012 Scripting Games, one of the events had to do with using performance counters. I thought that was a pretty good idea, so I started playing around with them. Then I saw your blogs about using the Out-GridView cmdlet, and I thought it would make sense to combine the two: performance counters and the Out-GridView cmdlet. The problem is that it seems to not work. So is this a known bug, or was it never intended to work together at all?
—CT
Hello CT,
Microsoft Scripting Guy, Ed Wilson, is here. Today is an awesome day. I had a great meeting with the TechEd Connect Zone people about the Scripting Guys booth at Microsoft TechEd 2012. TechEd 2012 will be in Orlando, Florida in the United States, and it will run June 11–14. I grew up in Florida, and I am excited to have the chance to go “home” again. In fact, the Scripting Wife and I are going to maximize our time in the sunshine state. I am speaking at SQL Saturday in Pensacola, Florida on June 9 on the way to Orlando. On the way home, we are stopping in Jacksonville, Florida on June 16 where I will be speaking at the Jacksonville IT Pro Camp.
The problem of Out-GridView and performance info
CT, using the Out-GridView cmdlet to view performance information in an easy-to-manipulate grid control is not a bad idea, and certainly, it works. The problem is that you need to understand a bit more about the underlying performance objects that are returned by the Get-Counter cmdlet.
Note I have written a number of blogs about using Windows PowerShell and performance counters. Please refer to this list of blog posts for additional information about this extremely important subject.
CT, if you pipe the direct results of the Get-Counter cmdlet to the Out-GridView cmdlet, you end up with a complex object in the CounterSamples field. This effectively limits what you can do with the grid control. Here is an example of what you are probably attempting to do (I have used ogv as an alias for the Out-GridView cmdlet).
Get-Counter -ListSet processor | Get-Counter | ogv
The resulting grid control is shown here. Note that when I attempt to use the filter for the grid control, nothing filters based on the string User. This indicates the inability of the grid control to effectively filter output.
Examine the members of the object
To provide the Get-GridView cmdlet with access to the specific counter information, it is first required to determine where the information resides. To look inside the PerformanceCounterSampleSet merely demands piping the result from the Get-Counter cmdlet to the Get-Member cmdlet. When we examine the members of the PerformanceCounterSampleSet object, two properties appear promising. The members of the PerformanceCounterSampleSet object are shown here.
PS C:\> Get-Counter -ListSet processor | Get-Counter -MaxSamples 1 | Get-Member
TypeName: Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet
Name MemberType Definition
—- ———- ———-
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
CounterSamples Property Microsoft.PowerShell.Commands.GetCounter.Performanc…
Timestamp Property System.DateTime Timestamp {get;set;}
Readings ScriptProperty System.Object Readings {get=$strPaths = “”…
The first property that appears promising is the Readings property. To examine further the Readings property, expand it and send the output to the Get-Member cmdlet. This technique is shown here.
PS C:\> Get-Counter -ListSet processor | Get-Counter -MaxSamples 1 | select -ExpandProperty readings | Get-Member
TypeName: System.String
Name MemberType Definition
—- ———- ———-
Clone Method System.Object Clone()
CompareTo Method int CompareTo(System.Object value), int Co…
Contains Method bool Contains(string value)
CopyTo Method System.Void CopyTo(int sourceIndex, char[]…
EndsWith Method bool EndsWith(string value), bool EndsWith…
Equals Method bool Equals(System.Object obj), bool Equal…
GetEnumerator Method System.CharEnumerator GetEnumerator()
GetHashCode Method int GetHashCode()
GetType Method type GetType()
GetTypeCode Method System.TypeCode GetTypeCode()
IndexOf Method int IndexOf(char value), int IndexOf(char …
IndexOfAny Method int IndexOfAny(char[] anyOf), int IndexOfA…
Insert Method string Insert(int startIndex, string value)
IsNormalized Method bool IsNormalized(), bool IsNormalized(Sys…
LastIndexOf Method int LastIndexOf(char value), int LastIndex…
LastIndexOfAny Method int LastIndexOfAny(char[] anyOf), int Last…
Normalize Method string Normalize(), string Normalize(Syste…
PadLeft Method string PadLeft(int totalWidth), string Pad…
PadRight Method string PadRight(int totalWidth), string Pa…
Remove Method string Remove(int startIndex, int count), …
Replace Method string Replace(char oldChar, char newChar)…
Split Method string[] Split(Params char[] separator), s…
StartsWith Method bool StartsWith(string value), bool Starts…
Substring Method string Substring(int startIndex), string S…
ToCharArray Method char[] ToCharArray(), char[] ToCharArray(i…
ToLower Method string ToLower(), string ToLower(System.Gl…
ToLowerInvariant Method string ToLowerInvariant()
ToString Method string ToString(), string ToString(System….
ToUpper Method string ToUpper(), string ToUpper(System.Gl…
ToUpperInvariant Method string ToUpperInvariant()
Trim Method string Trim(Params char[] trimChars), stri…
TrimEnd Method string TrimEnd(Params char[] trimChars)
TrimStart Method string TrimStart(Params char[] trimChars)
Chars ParameterizedProperty char Chars(int index) {get;}
Length Property System.Int32 Length {get;}
As you can see, the resulting objects are strings. The conversion to a string might be useful in some situations, but it does not facilitate additional processing as easily as a different type of object. Therefore, examine the CounterSamples property. When it is piped to the Get-Member cmdlet, the CounterSamples property appears much more interesting than a simple string. This technique is shown here.
PS C:\> Get-Counter -ListSet processor | Get-Counter -MaxSamples 1 | select -ExpandProperty countersamples | Get-Member -MemberType property
TypeName: Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample
Name MemberType Definition
—- ———- ———-
CookedValue Property System.Double CookedValue {get;set;}
CounterType Property System.Diagnostics.PerformanceCounterType CounterType…
DefaultScale Property System.UInt32 DefaultScale {get;set;}
InstanceName Property System.String InstanceName {get;set;}
MultipleCount Property System.UInt32 MultipleCount {get;set;}
Path Property System.String Path {get;set;}
RawValue Property System.UInt64 RawValue {get;set;}
SecondValue Property System.UInt64 SecondValue {get;set;}
Status Property System.UInt32 Status {get;set;}
TimeBase Property System.UInt64 TimeBase {get;set;}
Timestamp Property System.DateTime Timestamp {get;set;}
Timestamp100NSec Property System.UInt64 Timestamp100NSec {get;set;}
To test the CounterSamples property to see if it contains the requisite information, use the command that is shown here.
Get-Counter -ListSet processor | Get-Counter -MaxSamples 1 | select -ExpandProperty countersamples | select path, timestamp, cookedvalue
When the command runs, it displays the output shown here.
To permit working with multiple performance counter sets, store the information that is gathered into a variable. When the variable contains the performance information, pipe the output to the Out-GridView cmdlet. (The % sign is an alias for the Foreach-Object cmdlet and ogv is an alias for Out-GridView).
$sample = Get-Counter -ListSet processor | Get-Counter -SampleInterval 1 -MaxSamples 10
$sample | % { $_.countersamples } | select path, timestamp, cookedvalue | ogv
When the grid-view control appears, use either the quick Filter or the Add Criteria technique to parse the resulting performance information. The following image illustrates using the Add Criteria technique to find only cooked values that are greater than 100 and paths that contain the word interrupts. Because the performance counter paths are very descriptive, it is easy to find the desired information.
CT, that is all there is to using the Out-GridView cmdlet with performance counter information. Join me tomorrow for the Weekend Scripter.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
0 comments