In this example, there were three states for mailbox moves when they are created with the New-MoveRequest cmdlets. These are Completed, InProgress and Queued and because I only care about the first, we can effectively ignore any other types. With the script we need to look for the Conpleted state and wait til there are no other mailboxes with a state other than ‘Completed’.
The Code
As you can see by the code below, the script is fairly short. We use a few tricks to do this kind of check. First we run Get-MoveRequest to get all move requests in Exchange. We then add a ‘Where’ condition that looks for a criteria we determine. In our case, we are looking for a Status property of the Move Request that is not equal (-ne) to ‘Completed’. We then need to see how many of these there are, and we can do so by wrapping the query in ‘( ).Count’ which will present a tally of results that meet the criteria of the PowerShell cmdlets query we wrote. We then store that value in a variable, which is in this case ‘$UncompletedCount’.
We then add a line that visually indicates that we are still waiting (write host) and use the color yellow to highlight this. Additionally we wrap the entire set of code with a DO { } While () loop that allows us to keep checking while the $UncompletedCount does not equal 0. The reason is that when that variable is 0, there are no move requests that are in a state other than completed. That it. Here’s the code:
Do { $UncompletedCount = (Get-MoveRequest | Where {$_.Status -ne 'Completed'}).Count Start-Sleep 5 Write-Host 'Waiting' -ForegroundColor Yellow } While ($UncompletedCount -ne 0)