2013 Scripting Games Event 2

As I write this post, Event 2 winners have been announced. Event 3 voting period started and is opened till 2013-05-21 00:00:00 (GMT). In other words, we just passed mid-point in Scripting Games 2013

My entry for Event 2 in Advanced category was awarded place #5 by the voters’ community.

… and it was ranked #1 by the expert judges and the mighty panel of celebrity judges

I’d like to thank first all the anonymous voters on my entry, those who left a comment on my entry (it’s important to get feedback).
I’d like to address a special thank to the judges. As I’m also a voter, I understand now how difficult and time consuming it may be to appreciate others’ work and leave a comment that help others improve their PowerShell scripting skills.

My entry for Event 2 in the Advanced category won: http://scriptinggames.org/entrylist.php?entryid=465
I’m sure it didn’t win because it was superior to others, I’ve seen some greats entries and learned many things by looking, voting and commenting these entries.
I believe it won because there are probably some great learning points in it (hopefully, that’s what you’ll find).
I actually wrote this entry twice.
When I wrote my first version, I wanted to keep it simple. I started by the beginner entry instructions, wrote and validated some commands.
Then I changed it to make it an advanced function. As I was using the Win32_ComputerSystem WMI class, I also read the following limitations on this MSDN page:

I’ve left the code 2 days and before testing it on various systems, I read the instructions again. I didn’t pay enough attention the first time. I missed the “reliable” word.
We were supposed to find reliable info whatever the operating system. I googled the following keywords “WMI list sockets” and found this page
I had a look at these two knowledgebase articles:

After reading the “more information” section of these KB articles, the word “reliable” in the instructions made fully sense.
I decided to test every versions of Windows, check if the KB is present for Windows 2003 and XP… I actually even used the word “reliable” as a variable name to make sure the logic in the script is easier to follow.
I still wanted to keep it simple and easy to read. That’s why I implemented a huge switch block for all the versions of Windows.
What’s cool with this $Reliable variable is that it has 2 types depending on the OS version, either a boolean or an array of WMI object with 1 element or none.

The $Reliable could be $null if the specific KB isn’t found by querying the Win32_QuickFixEngineering WMI class.
The $Reliable could be hold 1 System.Management.ManagementObject#\Win32_QuickFixEngineering object if the specific KB is retreived from WMI.
This trick allowed me to just use a single if statement block.

if ($Reliable) {            
    Write-Verbose -Message "Reliable set to true"            
    $CS = Get-WmiObject -Class Win32_ComputerSystem  -Property $CSprops @WMIHT            
    $ProcessorsCount = $CS.NumberOfProcessors            
    $CoresCount      = $CS.NumberOfLogicalProcessors            
} else {            
    Write-Verbose -Message "Reliable set to false"            
    $CS = Get-WmiObject -Class Win32_ComputerSystem  -Property "TotalPhysicalMemory","NumberOfProcessors" @WMIHT            
    $CoresCount      = $CS.NumberOfProcessors            
    $ProcessorsCount = 0            
}

To make sure to understand how this work, you can test the following in your console:

if ($true) { $true } else { $false }            
            
if (0) { $true } else { $false }            
            
if (1) { $true } else { $false }            
            
# test an empty array            
if (@()) { $true } else { $false }            
            
# test an array with 1 element            
if (@('1')) { $true } else { $false }


As you can see in the above image, Mike F Robbins and Francois-Xavier Cat, the top 2 crowdscores on Advanced Event 2, already wrote blog articles that you can find here:

You can also read and learn many things from the Expert judges:

or by other competitors blogging on Scriptings Games that Mike started referencing on this landing page:

In case you missed it, a motivating prizes list is available on this page: http://powershell.org/wp/2013/04/17/scripting-games-2013-prize-list/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s