Do-Until within Try-Catch block

Hi,

Following on from the thread started here: Assign o365 Product Licence only where not already assigned I thought it better to start a new one as the question is for a different problem within this.

I have the following script block which enables services and a product within o365. I’m using a try-catch block to provide accurate information on a non-terminating error where a product is already assigned. This works well but want to add further checking to check that the mailbox has been setup before proceeding onto the next user so have included a do-until block. This is the second try/catch embedded statement shown below and again appears to work well but as before, would like to catch the errors shown when running “Until (Get-Mailbox -Identity $Upn)”. You can see my attempt of this below but the errors are still shown rather than the Catch statement.

Import-Module activedirectory
$TargetUsers = Get-ADUser -Filter * -SearchBase "OU=To Migrate,OU=CompanyUsers,OU=Company,DC=domain,DC=local"

foreach ($User in $TargetUsers) {
    $Upn = $User.UserPrincipalName
    Set-MsolUserLicense -UserPrincipalName $Upn -LicenseOptions $O365EntE3
    Write-Host "All Office 365 Enterprise E3 services enabled for " -f Cyan -NoNewline; Write-Host "$($Upn)" -f White
        Try
        {
            Set-MsolUserLicense -UserPrincipalName $Upn -AddLicenses tenent:EMS -ErrorAction Stop            
            Write-Host "EMS Product enabled for " -f Cyan -NoNewline; Write-Host "$($Upn)" -f White
        }
        Catch
        {
            Write-Warning "EMS Licence already assigned to $($Upn)"
        }
            Try 
            {
                Do
                { 
                    Start-Sleep -Seconds 15
                }
                Until (Get-Mailbox -Identity $Upn -ErrorAction Stop) 
                Write-Host "$($Upn) Mailbox ready for migration`n" -f Green
            }
            Catch
            {
                Write-Warning "$($Upn) mailbox not yet ready"
            }
    }

Am I missing something here?

Cheers
Jamie

Not sure if this will help but… Have you tried using Write-Warning on your inner catch ? also this article may help dealing with nested try/catch

https://powershell.org/forums/topic/nested-try-catch-error-message-from-nested-catch-block/

As far as I can see I’m already using Write-Warning in my nested catch or am I misunderstanding?

I see Write-Host

Do
{
Start-Sleep -Seconds 15
}
Until (Get-Mailbox -Identity $Upn -ErrorAction Stop)
Write-Host “$($Upn) Mailbox ready for migration`n” -f Green
}
Catch
{

Ah yes sorry. That’s not the issue as this message is displayed as expected only after the mailbox is ready for use. I’m trying to catch the error from the command:

Until (Get-Mailbox -Identity $Upn -ErrorAction Stop)

Just to make sure it’s not the cause I’ve commented out the write-host but this makes no difference other than not telling me it’s been successful.

The error I’m trying to catch is simply where it’s telling me that the mailbox doesn’t yet exist:

The operation couldn't be performed because object 'o365test1@mydomain.com' couldn't be found on 'FDASF07A4546DC06.EURPDSAFD46.PROD.OUTLOOK.COM'. + CategoryInfo : NotSpecified: (:) [Get-Mailbox], ManagementObjectNotFoundException + FullyQualifiedErrorId : [Server=AM4PR0G5H38,RequestId=1cfds63-68jb-467c-b6db-6d9vfd6997d,TimeStamp=07/07/2017 08:51:57] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] CF892878,Microsoft.Exchange.Mana gement.RecipientTasks.GetMailbox + PSComputerName : outlook.office365.com

I’ve even tried moving this out of the current foreach loop and isolating this as below but still no cigar:

$TargetUsers | ForEach-Object {
Try 
{
Do
 { 
    Start-Sleep -Seconds 15
 }
Until (Get-Mailbox -Identity $_.UserPrincipalName -ErrorAction Stop)
Write-Host "$($_.UserPrincipalName) Mailbox ready for migration`n" -f Green
}
Catch
{
Write-Warning "$($_.UserPrincipalName) mailbox not yet ready"
}

I’m pretty certain that it’s to do with a do-until loop within a try-catch that it’s not liking. I’ve tried other combinations with the same result so a little stumped.