11

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?

3
  • can you post what Get-Migrationbatch -Identity $MigrationBatchName gets you? You also using or wrong, may be why you are getting the issue. Also does this work with just Where {$_.Status -like "Completed"} Commented Sep 15, 2017 at 11:57
  • "Get-Migrationbatch -Identity $MigrationBatchName" was returning the job specified in the variable but with a status of "Syncing". Just using "Where {$_.Status -like "Completed"}" did work but thought I'd got this working correctly previously. How should I use -or then? Commented Sep 15, 2017 at 12:00
  • See Also: How to use Powershell Where-Object like an IN statement Commented Oct 5, 2021 at 12:07

3 Answers 3

30

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 })
Sign up to request clarification or add additional context in comments.

9 Comments

You beat me to it.
Ah nice one thanks. I'd already tried this method but was using -eq rather than -like....doh! I did start playing around with an array as it goes but again, forgot to use the -contains switch instead of -like. I've just tried the above array method but not getting any result at all!
What version of PowerShell are you using?
PSVersion 5.0.10586.117
Weird, I did test a mocked version of the second script... if the array approach isn't working for you, ask another question and we'll have a look :)
|
18

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")

2 Comments

so simple and beautiful.
great use of this workaround instead of multiple -or -and conditions
4

another option convert filter array to regex string...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.