where-object $_ 匹配多个条件 [英] Where-object $_ matches multiple criterias

查看:101
本文介绍了where-object $_ 匹配多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所以我正在尝试获取版本号.但是,Description1 可以有两个我想要查找的名称.我已经让我的代码只匹配一个字符串,但我似乎找不到正确的语法来匹配多个带有-or"的字符串

解决方案

这应该可以满足您的需求,而且比您原来的要短一些.

$data |哪里-对象{$_.Name -eq "$serverName.chrobinson.com" -and ($_.Description1 -match "bnx2x" - 或$_.Description1 -匹配be2net")} |选择对象 - 展开版本

您只需要 $_.Description1 -match "bnx2x" -or $_.Description1 -match "be2net" 真的,但我认为这比您拥有的更容易阅读.>

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

解决方案

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.

这篇关于where-object $_ 匹配多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆