Expert Commentary: 2012 Scripting Games Advanced Event 10

Expert Commentary: 2012 Scripting Games Advanced Event 10

  • Comments 3
  • Likes

Summary: Microsoft MCC, Rich Prescott, provides expert commentary for 2012 Scripting Games Advanced Event 10.

Microsoft Scripting Guy, Ed Wilson, is here. Rich Prescott is the expert commentator for Advanced Event 10.

Photo of Rich Prescott

Rich is currently working as a Windows Engineer for a market-leading global financial services firm in New York City. He started learning Windows PowerShell in 2009 while he was working as a desktop engineer. He is an author for the Microsoft TechNet Wiki. He is also a moderator on the Official Scripting Guys Forum, and he recently received the Microsoft Community Contributor award.

Personal blog: Engineering Efficiency
Microsoft blog: WikiNinjas - Official Blog of TechNet Wiki
Microsoft profile: Rich Prescott on TechNet
Twitter: @Rich_Prescott

Part one: The manual way

Having been a server admin previously, I have dealt with misbehaving servers on multiple occasions. Typically this led to creating a data collector set in Performance Monitor, and then viewing a chart with the information or exporting the information to a BLG file for later viewing.

Image of menu

When we had the BLG file, it could be converted into a CSV file by using the relog executable. This would create a CSV file that could satisfy three out of four of the requirements for Advanced Event 10.

Relog.exe Processor.blg –f CSV –o LT_processorCounters.csv

But this is the 2012 Scripting Games, and what would it be without some scripting!

Part two: The one-liner

After loading the Windows PowerShell console, I checked what available options there were for counters by using the following command:

Get-Command *counter*

Image of command output

After checking out the Get-Help listings for the Get-Counter and Export-Counter cmdlets, I was able to create a simple one-liner to generate the required CSV file:

 Get-Counter -Counter (Get-Counter -ListSet Processor).Paths -MaxSamples 10 -SampleInterval 2 |

 Export-Counter -Path "$($Env:ComputerName)_processorCounters.csv" -FileFormat csv

To fulfill the requirement of placing it in the Documents special folder, we can use Join-Path and a shortcut for the user’s documents folder.

 Get-Counter -Counter (Get-Counter -ListSet Processor).Paths -MaxSamples 10 -SampleInterval 2 |

 Export-Counter -Path (Join-Path ([environment]::getfolderpath("mydocuments")) "$($Env:ComputerName)_processorCounters.csv") -FileFormat csv

Image of menu

Now we have a mirror CSV file to what we created by using Relog.exe. However, if you read the Event Scenario carefully, it states that you must capture snapshots at two-second intervals for a total of ten snapshots, and there appears to be a row missing in both of the CSV files that we generated. If we wanted to be lazy, we could simply increment the MaxSamples parameter by 1, delete the first empty row in the CSV file, and call it a day. But this is the 2012 Scripting Games, and if you are going to do something, do it right!

Part three: The advanced way

In my testing, I found that the Get-Counter cmdlet was pulling all of the information correctly, but the Export-Counter and Relog.exe were not correctly outputting the data. To get around this, we can use Get-Counter to pull all of the information required, parse through it ourselves, and then output to a CSV file. I created the following function, which creates an array, loops through the objects piped from Get-Counter, converts them into custom PSObjects, and adds them to the array.

function Convert-RPCounter {

 

  Param(

    [Parameter(Mandatory=$true,ValuefromPipeline=$true)]

    $PerfData

    )

 

Begin {$Results = @()}

 

Process

  {

  ForEach ($Sample in $PerfData){

    $obj = New-Object PSObject -Property @{TimeStamp = $Sample.TimeStamp}

    ForEach($CounterSample in $Sample.CounterSamples){

      $obj | Add-Member -MemberType NoteProperty -Name $CounterSample.Path -Value $CounterSample.CookedValue

      }

    $Results += $obj

    }

  }

 

End {$Results}

 

}

We then call the function as follows:

Get-Counter -Counter (Get-Counter -ListSet "Processor").Paths -MaxSamples 10 -SampleInterval 2 |

 Convert-RPCounter |

 Export-Csv -Path (Join-Path ([environment]::getfolderpath("mydocuments")) "$($Env:ComputerName)_processorCounters.csv") -NoTypeInformation

Image of menu

As you can see in the previous image, we now have 10 full samples at two-second intervals as required. And that’s all there is to capturing performance data with Windows PowerShell.

Now starts the fun process of troubleshooting what is eating up all of my CPU…

…mumbles to himself, “Where is that bothersome Script Monkey? I thought I told him to stay away from my computer!”

~Rich

WOW! This also concludes the 2012 Scripting Games. I want to especially thank all of the judges and the expert commentators for all of their hard work. I also want to thank Dia Reeves (my editor) who has done much more than basic editing these past several months leading up to the Scripting Games. I also want to extend a special thank-you to our wonderful sponsors for all their support of this great event. I hope you had fun, and that you also found it to be a worthwhile learning endeavor.

Join me tomorrow as we return to our regularly scheduled Weekend Scripter. I promise that I will not talk about the Scripting Games tomorrow—well, maybe a little. By the way, if you happen to be in Virginia Beach, VA tomorrow, the Scripting Wife and I will be at the Mark Minasi Conference where I (along with Don Jones) will be speaking. With people like Mark Minasi and Don Jones on the agenda, how can it fail to be an exciting and fun time? Hope to see you there.

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

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

    a good explanantion and a comprehensible script!

    I like the advanced version, but the short version was required.

    As I'm using the german version of WIN7(64) I can tell you, that there are several (I hate it) language dependencies :-(((

    First of all : "Processor" is named "ProZessor" in german, so I have distinguished this noun depending on the OS language. "English" and "German" have been predefined, other languages require the user to enter the correct expression by "Read-Host".

    Next, the list seperator for Excel is depending on the regional settings. I read it from the registry and wrote a function to change it from "," to ";" in this case. You can use the -UseCulture switch to take this into account for Export-CSV but (why?) not for Export-Counter :-(

    You should add the -force switch to the Export Cmdlets to avoid an error message.

    Another thing to mention: "readable" output was one design point!

    This one "broke my neck" because I introduced a function a get rid of the displayed "###########" cells in excel using AutoFit() for the used range ( but I did upload an incorrrect version of this function that did cost at least 2 points )

    So ... to meet all the design points, a one liner isn't enough and I still neclected error handling completely!

    @Ed and Teresa: Have a nice time at the Mark Minasi Conference! I hope you can get the #2012sg out of your minds and relax a bit ... it's good to be ... back to normal, isn't it ... :-)))

    Klaus (Schulte)

  • Excellent, love the function.

    Out of interest how many points would part two earned you?

    John

  • @Klaus The way that I interpreted the event is that the screenshot shown was 'acceptable', but not optimal.  Essentially, it did not meet the requirements set by the boss of gathering 10 snapshots of 2 second intervals, as it only gathered 9 snapshots.  The last two "additional points" were somewhat contradictory of each other in my opinion.  The one-liner did not meet the *requirements* set by the boss, which supersede the "one-liner additional points", and is why I wrote a custom function.

    @John That depends on the judge.  :)  With my *interpretation* of the event and requirements/extras, you would get two stars for getting the output shown in Ed's screenshot.  If you were able to do it in a one-liner, that is an extra star.  If you met all of the requirements and extras (minus the last one), it would be five stars.  Anything in between would be four.  Again, that is my interpretation and I am only one judge, other may have graded differently.