Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 其中,对象$匹配多个标准_Powershell - Fatal编程技术网

Powershell 其中,对象$匹配多个标准

Powershell 其中,对象$匹配多个标准,powershell,Powershell,所以我想得到版本号。但是,Description1可以有两个我想要查找的名称。我已经让我的代码只匹配一个字符串,但我似乎找不到正确的语法来匹配多个带“-或“的字符串。这应该可以满足您的需要,并且比您最初使用的要短一些 $data|where-object{$_.Name -eq "$serverName.domain.com"}|select-object -Property Description1, Version | where-object{$_.Description1 -match

所以我想得到版本号。但是,Description1可以有两个我想要查找的名称。我已经让我的代码只匹配一个字符串,但我似乎找不到正确的语法来匹配多个带“-或“

的字符串。这应该可以满足您的需要,并且比您最初使用的要短一些

$data|where-object{$_.Name -eq "$serverName.domain.com"}|select-object -Property Description1, Version | where-object{$_.Description1 -match "bnx2x" -or "be2net"} | %{"{0}" -f $_.Version}
实际上,您只需要
$.Description1-匹配“bnx2x”-或$.Description1-匹配“be2net”
,但我认为这比您拥有的更容易阅读。

或者:

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

非常感谢。这正是我需要的。感谢您与我分享您的知识并详细说明~我有一个与此示例相关的问题:假设要匹配的字符串被通过函数传入的变量替换。你能想象一个场景,只有第一个,第二个变量在“-或”部分得到尊重吗?也许我必须为此单独提出一个问题,但没有找到一种方法来提出与此相关的问题。@user637338很抱歉这么晚才回复。我不确定我是否理解你的问题。你是说“bnx2x”和“be2net”被先前由函数定义的变量替换了吗?还是你只想得到前两场比赛?你最好单独问一个问题来更好地定义事情。
$data|
where-object{ ($_.Name -eq "$serverName.chrobinson.com") -and
              ($_.Description1 -match 'bnx2x|be2net') } | 
select -ExpandProperty Version