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

Expert Commentary: 2012 Scripting Games Beginner Event 10

$
0
0

Summary: Windows PowerShell MVP, Marco Shaw, provides expert commentary for 2012 Scripting Games Beginner Event 10.

Microsoft Scripting Guy, Ed Wilson, is here. Marco Shaw is the expert commentator for Beginner Event 10.

Photo of Marco Shaw

Marco has been in the IT industry for almost 15 years, and he is currently working for the IT consulting company CGI. He supports Windows servers and Linux, and he enjoys the various daily challenges he faces. He is a five-time recipient of the Microsoft MVP award for his support of Windows PowerShell online and offline communities.

First off, if I go back to last year—that was an “epic fail” on my part. I completely missed it. My solution didn’t even provide a solution to the problem. I will do better this year…

The first part of Beginner Event 10 is simple enough: get the performance counters. I already knew about Get-Counter because it is one of the core cmdlets in Windows PowerShell 2.0. It was simple enough to come up with:

get-counter -counter "\processor(*)\*" -sample 5 -max 3

However, it did take a bit of digging to get the counter parameter value right.

Next, I decided to tackle the “special documents folder.” I never pretend to know everything—not even close. I forget stuff all of the time, and I have to revert to using Bing a lot. I’m not embarrassed to say it. I’ve gotten used to it because I use multiple technologies at work, so I’m a “master of none.”

There is a way using .NET to get this special folder directly, but Ed made a point of adding a design point that indicates “use native Windows PowerShell commands.” If we were talking about more than a simple one-liner with a few cmdlets, I’d go with .NET, but I won’t this time. I played around a bit and came up with:

${env:userprofile}"\Documents\"${env:computername}"_ProcessorCounters.txt"

Not exactly pretty, but it meets the following requirements:

  • The documents folder
  • The server name in the output file
  • The file ending with _ProcessorCounters.txt

So, I naively went with:

get-counter -counter "\processor(*)\*" -sample 5 -max 3|add-content -path ${env:userprofile}"\Documents\"${env:computername}"_ProcessorCounters.txt"

Then I looked at the resulting text file to see the fruits of my labor—which turned out to be a lemon as shown here:

Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet

Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet

Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet

Ugh! The perfmon objects are getting written to the file, not their string-based counterparts. This simple change worked:

get-counter -counter "\processor(*)\*" -sampleinterval 5 -maxsamples 3|out-string|add-content -path ${env:userprofile}"\Documents\"${env:computername}"_ProcessorCounters.txt"

Success! I simply needed to pipe to Out-String to transform my objects into basic string objects, then pipe them to a text file.

~Marco

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