-2

I have a powershell script that determines a computer model number and attempts to filter by model family to run commands specific to model families. This is what I've got, but it doesn't work! #HP Inc. APR 2019

'$EliteBook=Get-WmiObject -Class:Win32_ComputerSystem -Filter:"Model LIKE '%HP EliteBook%'" -ComputerName:localhost'

'If($EliteBook)'

I first tried the to filter the model number excluding the prefix HP, but that didn't work either, any advice would be greatly appreciated!

  • Is there a particular reason why you're not using `Where-Object` to filter what you need? – Olaf Apr 28 '21 at 21:52
  • 2
    The single quotes surrounding each line, remove those. Also change `If($EliteBook)` to just `$EliteBook` or `If ($EliteBook) { $EliteBook }` – Daniel Apr 28 '21 at 22:09
  • Hi, I'm not sure how to use Where-Object, what should my code look like using Where-Object? – Arthur Durand Apr 28 '21 at 22:12
  • 1
    If you don't know how to use you should read the help [Where-Object](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.1). Please read it completely including the examples to learn how to use it. Regardless of that I'd recommend to use [Get-CimInstance](https://learn.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.1) instead of `Get-WmiObject`. Please read the help for that cmdlet as well. – Olaf Apr 28 '21 at 23:21
  • 1
    @ArthurDurand - as Daniel requested ... PLEASE remove the leading and trailing single quotes from each line of your posted code. that makes the code not work ... and it is also quite confusing. – Lee_Dailey Apr 28 '21 at 23:42
  • Aside from the distracting use of extraneous `'` characters and an incomplete statement in your post, there's no obvious problem with your attempt, except that it's unclear how it relates to the string `#HP Inc. APR 2019` mentioned in your post. – mklement0 Feb 21 '23 at 00:14

1 Answers1

1

Give this a try

A wildcard character in powerShell is '*'an asterisk If you have a lot of poissibilities to test for, you may want to use a Switch statemet rather than if()

$model = (Get-WMIObject -Class:Win32_ComputerSystem).Model

if($model -like "*EliteBook*") { Do SOmething }

Ernest Correale
  • 451
  • 3
  • 5
  • While this is indeed a viable _alternative_ to the OP's own attempt, there's no obvious problem with the OP's own attempt, which is generally preferable due to _filtering at the source_. While it is technically true that _PowerShell_'s wildcard character is `*`, it doesn't apply to using `Get-WmiObject`'s `-Filter` parameter, where _WMI_'s rules apply, and the equivalent of `*` is therefore `%`, as shown in the question. – mklement0 Feb 21 '23 at 00:21