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?

jshizzle
  • 467
  • 1
  • 11
  • 23
  • 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"}` – ShanayL Sep 15 '17 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? – jshizzle Sep 15 '17 at 12:00
  • **See Also**: [How to use Powershell Where-Object like an IN statement](https://stackoverflow.com/q/7179385/1366033) – KyleMit Oct 05 '21 at 12:07

3 Answers3

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 })
JamesQMurphy
  • 4,214
  • 1
  • 36
  • 41
  • 1
    You beat me to it. – ShanayL Sep 15 '17 at 12:05
  • 1
    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! – jshizzle Sep 15 '17 at 12:19
  • What version of PowerShell are you using? – JamesQMurphy Sep 15 '17 at 12:20
  • PSVersion 5.0.10586.117 – jshizzle Sep 15 '17 at 12:38
  • 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 :) – JamesQMurphy Sep 15 '17 at 16:50
  • so `contains` acts like `-like` and not like `-eq` ? – The Fool Nov 20 '19 at 22:14
  • @TheFool `-contains` is for collections, whereas `-like` and `-eq` are for values. `@(1,2,3) -contains 2` returns `$true`; `@(1,2,3) -contains 4` returns $false. – JamesQMurphy Nov 21 '19 at 03:35
  • Yes James, that is obvious. The question is whether `-contains` will look for an exact match. Your example suggests it behaves like `-like`. It is not! However, you can do it like this `$val -match ($array -join "|")` – The Fool Nov 21 '19 at 06:53
  • @TheFool Ok, now I understand what you are pointing out. Yes, `-contains` looks for an exact match. However, the OP was using the `-like` operator with operands that did *not* contain asterisks. In that instance, `-like` behaves just like `-eq`. – JamesQMurphy Nov 21 '19 at 16:08
17

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")
BobCormorano
  • 650
  • 1
  • 7
  • 14
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 "|")
Jason Phillips
  • 345
  • 1
  • 6