Determine Hardware Type – 2012 PowerShell Scripting Games Beginner Event #8

The details of the event scenario and the design points for Beginner Event #8 of the 2012 PowerShell Scripting Games can be found on the “Hey, Scripting Guys! Blog”.

Write a script to determine if a computer is a laptop or a desktop from a hardware prospective and display the output on the console. If this requires admin rights, you should detect if it is running as an admin or standard user. Extra points for writing a simple function that returns a boolean.

I kind of figured this was going to be a WMI thing since it involved hardware. Some research turned up an MSDN article on the Win32_ComputerSystem class which contained a PCSystemType property. You could also search through the WMI classes using Get-WMIObject -List *computer*.

Based on that MSDN article, only a value of 2 is a mobile computer (laptop). Based on some of the comments listed on the scenario requirements for this event, it appears to be safe to assume that all others are considered to be desktops.

I broke out my “Learn Windows PowerShell in a Month of Lunches” book and headed to chapter 19 to figure out how to write a simple function. Section 19.5 – Returning objects from a function was close enough to figure it out. I used If/Else and designed it so Else would never have to run unless the value was equal to 2.

Then all I had to do is call the function and display the output based on whether or not the function returned true or false with another If/Else statement. Here’s a link to my script on the PowerShell Code Repository site. The comments are interesting.

Update:
After I wrote this blog, but before it was published I received some peer feedback on this event (which I appreciate). Based on this feedback, it appears that I’m  returning the entire object instead of the single element (PCSystemType) as shown here:

This makes the function always return true.

The code in my script could have been written like this by expanding the property:

Here’s the complete script using this method (option #1):

Or by using the dotted notation (option #2):

Here’s the complete script using that method:

Or like this using the dotted notation in another way (option #3):

Now to solicit some feedback. Does anyone know which of these methods are preferred? Is one better than the other? Is there a best practice when it comes to this sort of thing?

I’m glad this error was caught since I didn’t realize there was an issue with the script I submitted. This allows me to learn from it and write better code in the future because the scripting games aren’t about winning or losing, they’re about learning. I want to remind the judges that this is why it’s so important to leave feedback. A three star rating with no comments means absolutely nothing! I led the beginner leaderboard on Day 5Day 6Day 7, and Day 8, but winning didn’t mean much to me since I already have an all expense paid trip to Microsoft Tech-Ed. This will be the third year in a row I’ve attended. That just leaves the bragging rights and bragging about being a beginner winner doesn’t mean much to someone whose been working as an IT professional for eighteen years. Next year I’ll be competing in the advanced competition and I think anyone who finishes in the top 10 of the beginner events should not be allowed to compete as a beginner, but should be required to compete in advanced if they chose to compete.

µ

2 Comments

  1. Sahal Omer

    Hi Mike,
    I prefer option two with a little modification
    Withou assigning to a variable call it inside the if block
    If((gwmi win32_computersystem -ea 0).pcsystemtype -ne 2)
    {Code here}
    Else
    {Code here

    Reply
  2. Mike F Robbins

    Thanks Sahal, that’s a great idea!

    Reply

Leave a Reply

%d bloggers like this: