PowerShell: Try/Catch does not work in version 5 with Exchange/Azure PS functions

Here is what I am doing

try{

Get-Mailbox -Identity ed@contoso.com -erroraction stop

Write-host “User exists”

}catch{

Write-host “User does not exist”

Continue

}

It works fine in PS 2,3,4 until I get to PS 5 where the erroraction will not work. The user does not exist, but it displays the error and moves on. I have tried $ErrorActionPreference=”stop” just before my try but it made no difference. Perhaps its time to move away from the try/catch? Anyway here is the work around I did and perhaps this is what i will have to do in place of try/catch.

$chkmbx=$null #Empty the checker variable before use else you may get some false results

$chkmbx=Get-Mailbox -Identity ed@contoso.com -erroraction silentlycontinue

if ($chkmbx){Write-host “User exists”

}

if (!($chkmbx)){

Write-host “User does not exist”

Continue

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s