Export Performance Counters with PowerShell to Ease Tuning

ScriptingGuy1

Summary: Learn how to use Windows PowerShell to export performance counter sets to simplify analysis and tuning.

Hey, Scripting Guy! Question

Hey, Scripting Guy! I think that using Windows PowerShell to parse performance counter stuff is pretty cool. However, as much as I am a command line person, there are times when I need to be able to use the GUI. For example, often I need to take a picture for a report I am writing. The report may be justification for my request to purchase additional hardware. As you know, a picture is worth a thousand words, and I am a person of few words. Is there a way that I can use Windows PowerShell to capture performance data, but still be able to perform analysis using graphical tools?

—LH

Hey, Scripting Guy! AnswerHello LH,

Microsoft Scripting Guy, Ed Wilson here. It is warming up a little bit here in Charlotte, North Carolina today. Of course, since it is winter, the snow replacement is rain. That is OK, however, because it makes for a nice, albeit somewhat gloomy, day. I have six hours of meetings today, but the meetings are cool, and I get to talk to some of my friends, so I do not mind. It does mean that I am up early so I can check the Scripter@Microsoft.Com email. With the volume of email I get, I cannot skip too many days before I am overwhelmed. I have a nice cup of English Breakfast tea steeping in my ceramic tea pot, and I am waiting for a couple of hard boiled eggs to come out of our new egg cooker. In the past, I would manually cook eggs. It involved filling a pot with water, covering it while it boiled, placing the eggs in the boiling water, waiting for about 30 minutes while they soaked, removing them from a pan, placing them in a pan full of ice to cool them down, and then finally peeling them. It is a process that consumed nearly an hour. This is fine for a weekend morning, but not too practical for a day when one has six hours worth of meetings; thus the automation of the egg via the egg cooker. Perhaps I can use the following code to automate my egg.

Get-Egg –uncooked | Set-Egg –property cooked –value $true

LH, working with performance counters can be a little like cooking a hardboiled egg. It can be fun, but at times automation needs to enter into the equation. If I am using the Get-Counter cmdlet to retrieve performance counter information, and I wish to have the ability to examine the data in the Performance Monitor tool, all I need to do is to pipe the results to the Export-Counter cmdlet. In the command that follows, I use the Get-Counter cmdlet to retrieve performance counters related to memory. I collect four samples at one-second intervals. I export the collected information to a blg formatted file in the C:\fso folder on my computer. This command appears here. (The following command is a single logical command. I added the line continuation character (`) at the end of the first line to break the line to fit the blog format. When the command is typed into the Windows PowerShell console, the line continuation is not required when the command wraps to additional lines.)

The use of the Get-Counter cmdlet appears in Monday’s Hey Scripting Guy! Blog post.

PS C:\> Get-Counter -Counter (get-Counter -ListSet memory).paths -MaxSamples 4 `

-SampleInterval 1 | Export-Counter -Path c:\fso\memory.blg -FileFormat blg

PS C:\>

No output displays in the Windows PowerShell console when the previous command runs. The only way I know the command completed successfully is the absence of error messages. The only way I know that the command completed running is the presence of a fresh Windows PowerShell console command prompt.

I can open the Performance Monitor utility (Start/All Programs/Administrative Tools/Performance Monitor or Start/Search Perfmon <ENTER>), and import the saved blg file. To do this, I right-click Performance Monitor in the left pane and select Performance Monitor Properties as shown in the following screen shot.

Image of Performance Monitor Properties dialog box

The strange thing is that when I press OK, it appears that nothing has happened. This is shown in the following screen shot.

Image of Performance Monitor result

If I look closely, however, I will notice that something has changed—there is no longer a thin red line traversing the bottom of the monitor display. That small red line was the total percentage of processor utilization real-time display. The fact that it no longer displays indicates that the data source has changed. Now it is a simple matter of adding data to the display. I click the green plus (+) sign on the Tool menu to add the counters. Only counters that are available from the log file are present in the Add Counter dialog box. This is shown in the following screen shot.

Image of Add Counter dialog box

After I have selected the counters that I am interested in viewing, the graph displays the recorded data. On the other hand, if I would like to view current data, I can click the small graph icon on the tool menu. This icon is a bit small, and in the following screen shot, I have drawn an arrow to indicate the graph to which I refer.

Image showing graph icon

There may be times that I want to be working with my data in Microsoft Excel. The easy way to do this is to export the data as a CSV file. To do this I pipe the data collection that is returned from the Get-Counter cmdlet to the Export-Counter cmdlet and specify a FileFormat value of csv. (The following command is a single logical command. I added the line continuation character (`) at the end of the first line to break the line to fit the blog format. When the command is typed into the Windows PowerShell console, the line continuation character is not required when the command wraps to additional lines.)

PS C:\> Get-Counter -Counter (get-Counter -ListSet memory).paths -MaxSamples 4 `

-SampleInterval 1 | Export-Counter -Path c:\fso\memory.csv -FileFormat csv

PS C:\>

When I double-click the csv file, it opens in Microsoft Excel, and I can then manipulate the data. This is shown in the following screen shot.

Image of Excel data

One problem with extensive performance monitoring, is that the counter log files can become rather large. To ensure that the counter log files do not eat up all of the disk space, I can use the MaxSize parameter with the Export-Counter cmdlet. When the log file reaches the maximum size an error is raised, and displayed to the Windows PowerShell console. The raised error is a non-terminating error, and it is more informational than an actual problem. The MaxSize parameter is an integer, and it is measured in megabytes (MB). Therefore, a MaxSize of 1 will create a 1 MB (1024 KB) file in size.

Proper planning of a data gathering session includes selecting the counters, and selecting the interval and number of samples. A test should then be run to determine the size of the log file. From this baseline, the appropriate value of the maximum log file is calculated. When I run performance gathering sessions and I intend to analyze immediately the data, I use the force switch to cause the Export-Counter cmdlet to overwrite a previously existing log file of the same name. If I am only interested in collecting the most recent data, I use the circular switch and thereby cause the log file to begin overwriting the oldest records when the log file reaches maximum size. The following command obtains the memory performance counters, and attempts to collect 4,000 samples at 1 second intervals. These are then exported to a blg file format, but they are limited to a maximum size of 1 MB.

PS C:\> Get-Counter -Counter (get-Counter -ListSet memory).paths -MaxSamples 4000 `

-SampleInterval 1 | Export-Counter -Path c:\fso\memory.blg -FileFormat blg -MaxSize 1

When the log file is full, an error appears in the Windows PowerShell console. This error is shown in the following screen shot.

Image of error message

One of the things I enjoy doing with the Export-Counter Windows PowerShell cmdlet is using it to perform file type conversions. I can convert a blg file (generated via Export-Counter as illustrated earlier, or saved from the Performance Monitor utility) to a CSV file format or to a tab separated value (TSV) file format. To convert a blg file to a different format, I use the InputObject parameter. Here I use the Import-Counter cmdlet to import the blg file and to convert it to a PerformanceCounterSampleSet object. I then specify the path to store the converted file, as well as specify the FileFormat parameter. The following command accomplishes these tasks by importing a blg file that was created earlier, and converting the format to a TSV file. (The following command is a single logical command. I added the line continuation character (`) at the end of the first line to break the line to fit the blog format. When the command is typed into the Windows PowerShell console, the line continuation character is not required when the command wraps to additional lines.)

PS C:\> Export-Counter -InputObject (import-counter c:\fso\memory.blg) `

-Path c:\fso\memory.tsv -FileFormat tsv

PS C:\>

The newly created TSV file is shown in the following image.

Image of TSV file

LH, that is all there is to using the Windows PowerShell Export-Counter cmdlet. This also concludes Performance Week.

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

Discussion is closed.

Feedback usabilla icon