It works...but is it right ?

As part of our OSD deployment we need to find a way of installing a bunch of “hotkey” for certain models. I’ve written this script tonight (Ignore the output result of Wrong or right, its a test !) and it works but something doesn’t feel right about the double curly braces on each line. Have i done this the correct way ?

# This script will detect the machine model and then install the hotkeys for that model


$model = (Get-CimInstance -Namespace ROOT\cimv2 -ClassName Win32_ComputerSystem).Model

Switch -Wildcard ($Model) {

 { $_ -contains "HP EliteBook 8460p" } { write-output "Wrong!"; break }
 { $_ -contains "HP EliteBook 8470p" } { write-output "Right!!"; break }
 { $_ -contains "HP EliteBook 2170p" } { write-output "Wrong!"; break }
 { $_ -contains "HP EliteBook 2570p" } { write-output "Wrong!"; break }
 { $_ -contains "HP EliteBook 840 G1" } { write-output "Wrong!"; break }
 { $_ -contains "HP EliteBook 840 G2" } { write-output "Wrong!"; break }

Default { "Unknown Model type" }
 
 }

Many thanks as always.

So… -contains isn’t really the right operator, no. It’s not a wildcard operator, which is how you seem to be using it. I think you’re after -like. -Contains is a bit more complicated, as it’s meant to look for items in a collection. Win32_ComputerSystem never actually turns a collection, and the Model property is just a string.

And you’re kinda not using the Switch construct correctly.

switch -wildcard ($model) {
  "HP EliteBook 8460p" { # do something }
  "HP EliteBook 8470p" { # do something }
}

That’s actually the structure I’d expect to see, given what I think you’re trying to achieve. The conditions on a Switch construct aren’t necessarily meant to be entire comparisons; Switch is what DOES the comparison, in this case a wildcard string comparison, since you’ve used -Wildcard. You’re comparing what’s in $Model with specific values. Now, as you’ve noted, what you’ve done can work - but the structure you’ve used is intended for more complex situations; for what you’re doing, it’s overkill.

Thank you Don that’s very helpful. It didn’t feel right so I’m glad I queried it. It’s hard learning powershell as you think you’ve grasped something and find its not used in the right way! By all this is very helpful and I appreciate your good self replying.