runcmd
runcmd

Reputation: 602

Where-object $_ matches multiple criterias

$data|where-object{$_.Name -eq "$serverName.domain.com"}|select-object -Property Description1, Version | where-object{$_.Description1 -match "bnx2x" -or "be2net"} | %{"{0}" -f $_.Version}

So I'm trying to get the version number. However, the Description1 can have two names that I want to look for. I've gotten my code to work for just matching one string but I cant seem to find the right syntax to match multiple strings with an "-or"

Upvotes: 12

Views: 122559

Answers (2)

TheMadTechnician
TheMadTechnician

Reputation: 36287

This should do what you want, and is a bit shorter than what you originally had.

$data | Where-Object{
  $_.Name -eq "$serverName.chrobinson.com" -and (
     $_.Description1 -match "bnx2x" -or
     $_.Description1 -match "be2net"
  )
} | Select-Object -expand version

You just needed $_.Description1 -match "bnx2x" -or $_.Description1 -match "be2net" really, but I think this is easier to read than what you had.

Upvotes: 23

mjolinor
mjolinor

Reputation: 68243

Alternatively:

$data|
where-object{ ($_.Name -eq "$serverName.chrobinson.com") -and
              ($_.Description1 -match 'bnx2x|be2net') } | 
select -ExpandProperty Version

Upvotes: 16

Related Questions