如果你有数组:
$requiredFruit= @("apple","pear","nectarine","grape")
$someFruit= @("apple","banana","pear","nectarine","orange","grape")
$moreFruit= @("apple","banana","nectarine","grape")
你可以得到一个布尔结果:
'Check $someFruit for $requiredFruit'
-not @($requiredFruit| where {$someFruit -notcontains $_}).Count
'Check $moreFruit for $requiredFruit'
-not @($requiredFruit| where {$moreFruit -notcontains $_}).Count
使用计数的数组防止单个值不匹配计算结果为假。例如:
# Incorrect result
-not (0| where {(1,2) -notcontains $_})
# Correct result
-not @(0| where {(1,2) -notcontains $_}).Count
使用PowerShell v3,则可以使用select -first 1
停止管道时,第一发现不匹配(在V2 select -first 1
只允许一个通过对象,但管道的前面元素继续处理)。
-not @($requiredFruit| where {$moreFruit -notcontains $_}| select -first 1).Count
@匿名懦夫:为什么downvote? – BACON 2016-01-15 18:22:42