Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Expert Commentary: 2012 Scripting Games Advanced Event 10

$
0
0

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


Viewing all articles
Browse latest Browse all 3333

Trending Articles