Hey, Scripting Guy! Can I Use Windows PowerShell 2.0 for Performance Monitoring?

Hey, Scripting Guy! Can I Use Windows PowerShell 2.0 for Performance Monitoring?

  • Comments 1
  • Likes

Bookmark and Share 

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I love using performance monitor. I was a big fan of it back in the Windows NT 4.0 days, and I have been using it ever since. It seems, however, that beginning with Windows Vista you people have messed it up. In trying to make it easier for a clueless individual to use, you have absolutely messed it up for the IT pros who know what they are doing. In fact, as it stands right now, I use a Windows Server 2003 server as my monitoring machine, and I connect to everything remotely. I capture my performance logs on that box, and do all of my analysis from that machine. I have it connected to a switch box on my desktop and it works pretty well. I do wish, however, that I could use Windows PowerShell to parse the log files. That would be cool. For now, I am content with using PerfMon to do this.

-- GM

 

Hey, Scripting Guy! AnswerHello GM,

Microsoft Scripting Guy Ed Wilson here. The Scripting Wife’s idea of convalescing down on the South Carolina coast in Murrells Inlet has been a capital idea. I take my laptop to the shore, make a recliner out of the sand, and plop down with a thermos of hot tea and write. It is a most relaxing way to work. Unfortunately, the South Carolina parks commission has not yet installed wireless access points on the beach, so I am having to use Outlook 2010 in offline mode, which works just great. I synchronize it in the morning, and in the evening when I return to the condo, and all is well.

GM, I was reading your e-mail about performance monitor, as I was watching the waves, and the sea gulls as they frolic in the sand. Because I am seldom without either my laptop or my camera, I am able to share a part of my experience with you. Here is a picture I snapped while writing this article. As you can see, it is a lovely day, especially at a time when much of the southern United States is still snowed in. I am sharing the beach with only a few sea gulls.

Image of seagulls on beach at Murrells Inlet

 

GM, I am not completely certain I understand what you are talking about when you say we ruined the performance monitor utility. For creating real-time graphs, it still works exactly the same way: Open PerfMon, add counters, and then look at the graph. An example graph is seen in the following image.

Image of graph generated by PerfMon

 

However, if you are talking about creating logs, and copying counter sets and the like, I will agree that the process is a bit more convoluted. On the other hand, for experts such as yourself, you really need to learn how to use the counter cmdlets that have been added to Windows PowerShell 2.0.

One of the things I really love about Windows PowerShell is that I can use Windows PowerShell to help me learn how to use Windows PowerShell. (Did I just blow your mind?) For example, I can use the Get-Command cmdlet to find Windows PowerShell cmdlets that are related to performance counters:

PS C:\> Get-Command -Noun *counter*

CommandType     Name                                                Definition
-----------     ----                                                ----------
Cmdlet          Export-Counter                                      Export-Counter [-Path] <String> [-FileFormat <St...
Cmdlet          Get-Counter                                         Get-Counter [[-Counter] <String[]>] [-SampleInte...
Cmdlet          Import-Counter                                      Import-Counter [-Path] <String[]> [-StartTime <D...


PS C:\>

As seen here, there are three Windows PowerShell cmdlets that can be used to work with performance counters. Let’s take some time to explore the Get-Counter cmdlet. The first thing to do is to use the listset parameter to retrieve information about performance counter sets. You can use wildcard characters to pull information about multiple performance counter sets, as shown in the following image.

Image of using wildcard character to pull information about multiple performance counters


After you have an idea of the counter set in which you are interested, you may wish to shift your focus to that particular counter set. This is shown here:

PS C:\> Get-Counter -ListSet 'processor information'


CounterSetName     : Processor Information
MachineName        : .
CounterSetType     : MultiInstance
Description        : The Processor Information performance counter set consists of counters that measure aspects of processor activity. The processor is the part of the computer that performs arithmetic and logical computations, initiates operations on peripherals, and runs the threads of processes. A computer can have multiple processors. The Processor Information counter set represents each processor as an instance of the counter set.
Paths              : {\Processor Information(*)\Processor State Flags, \Processor Information(*)\% of Maximum        Frequency, \Processor Information(*)\Processor Frequency, \Processor Information(*)\Parking Status...}
PathsWithInstances :{\Processor Information(_Total)\Processor State Flags, \Processor Information(0,_Total)\Processor State Flags, \Processor Information(0,3)\Processor State Flags, \Processor Information(0,2)\Processor State Flags...}
Counter            : {\Processor Information(*)\Processor State Flags, \Processor Information(*)\% of Maximum Frequency, \Processor Information(*)\Processor Frequency, \Processor Information(*)\Parking Status...}

PS C:\>


The Get-Counter cmdlet returns a CounterSet object. This is seen here when we pipe the object to the Get-Member cmdlet.

PS C:\> Get-Counter -ListSet 'processor information' | Get-Member


   TypeName: Microsoft.PowerShell.Commands.GetCounter.CounterSet

Name               MemberType    Definition
----               ----------    ----------
Counter            AliasProperty Counter = Paths
Equals             Method        bool Equals(System.Object obj)
GetHashCode        Method        int GetHashCode()
GetType            Method        type GetType()
ToString           Method        string ToString()
CounterSetName     Property      System.String CounterSetName {get;}
CounterSetType     Property      System.Diagnostics.PerformanceCounterCategoryType CounterSetType {get;}
Description        Property      System.String Description {get;}
MachineName        Property      System.String MachineName {get;}
Paths              Property      System.Collections.Specialized.StringCollection Paths {get;}
PathsWithInstances Property      System.Collections.Specialized.StringCollection PathsWithInstances {get;}


PS C:\>


We can therefore use dotted notation to retrieve the paths from the CounterSet object. This is seen here:

PS C:\> (Get-Counter -ListSet 'processor information').paths
\Processor Information(*)\Processor State Flags
\Processor Information(*)\% of Maximum Frequency
\Processor Information(*)\Processor Frequency
\Processor Information(*)\Parking Status
\Processor Information(*)\% Priority Time
\Processor Information(*)\C3 Transitions/sec
\Processor Information(*)\C2 Transitions/sec
\Processor Information(*)\C1 Transitions/sec
\Processor Information(*)\% C3 Time
\Processor Information(*)\% C2 Time
\Processor Information(*)\% C1 Time
\Processor Information(*)\% Idle Time
\Processor Information(*)\DPC Rate
\Processor Information(*)\DPCs Queued/sec
\Processor Information(*)\% Interrupt Time
\Processor Information(*)\% DPC Time
\Processor Information(*)\Interrupts/sec
\Processor Information(*)\% Privileged Time
\Processor Information(*)\% User Time
\Processor Information(*)\% Processor Time
PS C:\>


Because the path property returns a specific performance counter, such as \Processor Information(*)\Processor State Flags, you can pipe the results of the previous command to the Get-Counter cmdlet again, and return performance information from each of the performance counters. This is shown in the following image.

Image of returning performance information for each performance counter

 

GM, we will stop here. This should give you some ideas for playing around with the performance counter cmdlets in Windows PowerShell 2.0.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questionson the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi,

    Thanks for the good example. I had gone through the cmdlet and tried it on the powershell commandline. Is it possible to accomplish the same thing using C# (using get-counter cmdlet through c#)?

    -regards,

    Tanish S.