I'm using the following in a do-until block to loop until a specified Exchange Online migration status is present:
(Get-Migrationbatch -Identity $MigrationBatchName | Where {$_.Status -like "Completed" -or "CompletedWithErrors" -or "Corrupted" -or "Failed" -or "Stopped"})
However, the above still returns a job with the status of "Syncing" and so continues the script regardless.
I've tried -match, -eq but still the same.
What am I missing?
The Where-Object clause in PowerShell filters the content according to a specific value of the property of an object. The Where-Object clause can also be used with any cmdlet/function to target a specific property value of an object.
The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a particular ID, or computers that use a particular version of Windows.
The PowerShell Where-Object cmdlet's only goal is to filter the output a command returns to only return the information you want to see. In a nutshell, the Where-Object cmdlet is a filter; that's it. It allows you to construct a condition that returns True or False.
The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.
You have to write it like this:
(Get-Migrationbatch -Identity $MigrationBatchName | Where {($_.Status -like "Completed") -or ($_.Status -like "CompletedWithErrors") -or ($_.Status -like "Corrupted") -or ($_.Status -like "Failed") -or ($_.Status -like "Stopped")})
Here's another way to do it:
$valuesToLookFor = @(
'Completed',
'CompletedWithErrors',
'Corrupted',
'Failed',
'Stopped')
(Get-Migrationbatch -Identity $MigrationBatchName |
Where-Object { $valuesToLookFor -contains $_.Status })
It would be simpler using -in
operator, given that you are not using wildcards:
(Get-Migrationbatch -Identity $MigrationBatchName | Where Status -in "Completed","CompletedWithErrors","Corrupted","Failed","Stopped")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With