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*.

2012sg-be8-1.png

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.

2012sg-be8-2.png

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.

2012sg-be8-3.png

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.

function Get-HardwareType {

<#

.SYNOPSIS
Get-HardwareType is used to determine if a computer is a laptop of desktop.

.DESCRIPTION
Get-HardwareType is used to determine a computer's hardware type of whether or not the
computer is a laptop or a desktop.

#>

$hardwaretype = Get-WmiObject -Class Win32_ComputerSystem -Property PCSystemType
If ($hardwaretype -ne 2)
{
return $true
}
Else
{
return $false
}}

If (Get-HardwareType)
{
"$Env:ComputerName is a Desktop"
}
Else
{
"$Env:ComputerName is a Laptop"
}

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:

2012sg-be7-41.png

This makes the function always return true.

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

2012sg-be7-5.png

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

2012sg-be7-7.png

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

2012sg-be7-6.png

Here’s the complete script using that method:

2012sg-be7-9.png

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

2012sg-be7-8.png

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

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.

ยต