Thursday, 19 July 2012

LttP: Powershell Games 2012 - Beginner Event 10

Here goes my entry for Beginner Event 10:

for($i=0;$i -lt 3; $i++){(Get-Counter -ListSet Processor).Paths | Get-Counter | Out-File (Join-Path ([Environment]::GetFolderPath("mydocuments")) $env:COMPUTERNAME"_ProcessorCounters.txt") -Append;Start-Sleep 5}

I went for the "one-liner" option basically just to help me get into the habit of cleaning my own code up a bit.

This one was quite fun really, it took me a few tries with Out-File to realised I needed the "-Append", as I'm used to using Add-Content instead.

Also, the construction of the path threw me for a minute, until I remembered about "Join-Path".

Time to read what the Expert has to say :)

UPDATE:

The results are right, but the method is a bit...wonky.

Firstly, I could've just used the "-SampleInterval" and "-MaxSamples" switches with the "Get-Counter" cmdlet, instead of doing a For loop with a "Start-Sleep".

Secondly, "[Environment]::GetFolderPath("mydocuments")" isn't strictly native Powershell, it pulls in the .NET framework.  I was going to go with the way the Expert did, but I let my self-doubt creap in when I read the Scripting Wife's comments.

Unfortunately I was late to the party (LttP) so I won't ever really know how I would've been scored for real, but I've learned quite a bit and had some fun with it.

I'm really looking forward to the games next year, and then I'll see how I do for real!

No comments: