Home > Back-end >  PowerShell stop script when condition is met
PowerShell stop script when condition is met

Time:06-18

I can't see to find the key to this problem I'm having. I believe I'm using the correct loop for this application, however I cannot get the script to stop when the condition of Percent Complete is at 100%. The Do loop will run forever.

Import-Module ExchangeOnlineManagement

Connect-ExchangeOnline

$User = "[email protected]"

New-MoveRequest -Identity "$User" -Remote -RemoteHostName "remotemailboxaddress" -TargetDeliveryDomain "targetdomain.mail.onmicrosoft.com" -RemoteCredential (Get-Credential)

Do {Get-MoveRequestStatistics -Identity "$User" -InformationVariable $Mailbox |  Select-Object Displayname, StatusDetail, TotalMailboxSize, PercentComplete | Format-Table Displayname, StatusDetail, TotalMailboxSize, PercentComplete
            Start-Sleep -Seconds 3
    } Until ({Get-MoveRequestStatistics -Identity "$User" -InformationVariable $Mailbox |  Select-Object PercentComplete} -eq 100)


Run Disconnect-ExchangeOnline once completed with migration
Disconnect-ExchangeOnline

The output is almost how I'd like it as well. I would like for it to add a new line instead of recreating the entire table and of course stop once Percent complete hits 100%. The script will also skip right to Disconnect-Exchange online as well if it is not commented out.

DisplayName   StatusDetail        TotalMailboxSize        PercentComplete
-----------   ------------        ----------------        ---------------
SMITH, Thomas WaitingForJobPickup 53.49 KB (54,770 bytes)              95



DisplayName   StatusDetail        TotalMailboxSize        PercentComplete
-----------   ------------        ----------------        ---------------
SMITH, Thomas WaitingForJobPickup 53.49 KB (54,770 bytes)              95



DisplayName   StatusDetail TotalMailboxSize        PercentComplete
-----------   ------------ ----------------        ---------------
SMITH, Thomas ADUpdate     53.49 KB (54,770 bytes)              95



DisplayName   StatusDetail TotalMailboxSize        PercentComplete
-----------   ------------ ----------------        ---------------
SMITH, Thomas Cleanup      53.49 KB (54,770 bytes)              99



DisplayName   StatusDetail TotalMailboxSize        PercentComplete
-----------   ------------ ----------------        ---------------
SMITH, Thomas Completed    53.49 KB (54,770 bytes)             100



DisplayName   StatusDetail TotalMailboxSize        PercentComplete
-----------   ------------ ----------------        ---------------
SMITH, Thomas Completed    53.49 KB (54,770 bytes)             100

I've really tried to make it work, differnt loop conditions, tried setting a variable on the Get-MoveRequestStatistics line. No luck, any ideas?

CodePudding user response:

Select-Object outputs an object which contains the selected properties. To get the value of a property, use Select-Object -ExpandProperty PropertyName, ForEach-Object PropertyName or use member access (Expression).PropertyName.

You can remove the 2nd Get-MoveRequestStatistics call by capturing the output of the first one in a variable. Then the most natural way to get the PercentComplete property in the loop condition is to use member access.

do {
    Start-Sleep -Seconds 3
    $stats = Get-MoveRequestStatistics -Identity "$User" -InformationVariable Mailbox 
    $stats | Format-Table Displayname, StatusDetail, TotalMailboxSize, PercentComplete
}
Until( $stats.PercentComplete -eq 100 )

I've also removed the Select-Object because it is redundant. You already select which properties to show for the Format-Table command.

There was a little mistake: -InformationVariable $Mailbox. The variable name must be passed without $ prefix. Otherwise PowerShell would take the value stored in variable $Mailbox as the name of the variable to assign the information stream to. This is propably not what you intended.

  • Related