如果你有数组:
$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
使用数组的计数可以防止单个不匹配的值被评估为 False。例如:
# 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