Monday, February 25, 2008

Sudden Death Challenge Event 5

Event 5 for the Sudden Death Challenge was much more difficult than the previous events. I was able to solve it with less than 10 lines of code. Powershell is truly the future of MS admin scripting. My solution to Event 5 of the SD Challenge.

   1:  $list=gwmi -List |?{$_ -match "win32_"} | % {$_.PSBase.Properties }
   2:  97..121 | % {
   3:  $(for ($i=0;$i -lt $list.Length;$i++) {
   4:      if (($list[$i].name.substring(0,1) -eq [char]$_) -and ($list[$i].origin.substring(0,5) -eq "win32")) {
   5:      "$($list[$i].name)`t$($list[$i].origin)"}})[0]
   6:  }

First extracting all the classes that begin with "Win32_" and saving each classes Properties into a variable. Here's the tricky part. We need to match the first letter of any class and build a list from "a" to "y". Hence the array 97..121. This is the ASCII code for "a" to "y". I then build an array of any class that matches the first letter of its property with A for the first loop and is from the Win32 class. Then only selecting the first element of that array I continue the loop from "a" to "y".

No comments: