Where-object $_ matches multiple criterias

两盒软妹~` 提交于 2020-12-29 13:06:47

问题


$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"


回答1:


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.




回答2:


Alternatively:

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


来源:https://stackoverflow.com/questions/23896345/where-object-matches-multiple-criterias

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!