如果你有數組:
$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