PowerShell Where-Object喜欢多个字符串值

时间:2017-09-15 11:54:17

标签: powershell where

我在do-until块中使用以下内容循环,直到出现指定的Exchange Online迁移状态:

(Get-Migrationbatch -Identity $MigrationBatchName | Where {$_.Status -like "Completed" -or "CompletedWithErrors" -or "Corrupted" -or "Failed" -or "Stopped"})

但是,上述内容仍会返回状态为"同步"所以无论如何继续使用脚本。

我已尝试过-match,-eq但仍然相同。

我错过了什么?

3 个答案:

答案 0 :(得分:9)

你必须这样写:

(Get-Migrationbatch -Identity $MigrationBatchName | Where {($_.Status -like "Completed") -or ($_.Status -like "CompletedWithErrors") -or ($_.Status -like "Corrupted") -or ($_.Status -like "Failed") -or ($_.Status -like "Stopped")})

这是另一种方法:

$valuesToLookFor = @(
    'Completed',
    'CompletedWithErrors',
    'Corrupted',
    'Failed',
    'Stopped')

(Get-Migrationbatch -Identity $MigrationBatchName |
    Where-Object { $valuesToLookFor -contains $_.Status })

答案 1 :(得分:0)

假设您不使用通配符,那么使用-in运算符会更简单:

(Get-Migrationbatch -Identity $MigrationBatchName | Where Status -in "Completed","CompletedWithErrors","Corrupted","Failed","Stopped")

答案 2 :(得分:0)

另一个选择将过滤器数组转换为正则表达式字符串...

$filter_status = @("Completed", "CompletedWithErrors","Curropted","Failed", "Stopped")
(Get-Migrationbatch -Identity $MigrationBatchName | Where Status -Match ($filter_status -Join "|")