Doubt on handling non-terminating error

Hi,

I’ve been reading “The big book of PowerShell error handling” since Don Jones indicated it to me.

On page 9, the author says:
“A value of SilentlyContinue only adds the error to the $Error variable; it does not write the error to the Error stream (so it will not be displayed at the console).”

So I’m trying to do this on Office 365:

It wasn’t supposed to have 1 error?
Try/Catch/Finally won’t work since it’s a non-terminating error.

Tried using $ErrorActionPreference and it worked.
But when I’ve tried to run the script it didn’t worked =/

Inside the script, using “ErrorAction SilentlyContinue” works.
I still don’t know why… but I’m glad that it worked =]

Well… now it don’t work anymore… and I can’t figure why!

here comes the script:

Write-Host "Which is the mail address that you want to remove calendar's editor permissions?" -Foregroundcolor Yellow
Write-Host

$mail = Read-Host
Write-Host
Write-Host

Remove-MailboxPermission 01Room -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on 01Room"}
Remove-MailboxPermission 02Room -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on 02Room"}
Remove-MailboxPermission 03Room -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on 03Room"}
Remove-MailboxPermission 04Reuniao -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on 04Reuniao"}
Remove-MailboxPermission 05Room -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on 05Room"}
Remove-MailboxPermission 06Room -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on 06Room"}
Remove-MailboxPermission Notebook01 -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on Notebook01"}
Remove-MailboxPermission Notebook02 -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on Notebook02"}
Remove-MailboxPermission Equipment01 -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on Equipment01"}
Remove-MailboxPermission Equipment02 -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on Equipment02"}
Remove-MailboxPermission Equipment03 -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*não encontrado.*"){Write-Host "No permission for $mail on Equipment03"}

Remove-MailboxFolderPermission 01Room:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on 01Room"}
Remove-MailboxFolderPermission 02Room:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on 02Room"}
Remove-MailboxFolderPermission 03Room:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on 03Room"}
Remove-MailboxFolderPermission 04Reuniao:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on 04Reuniao"}
Remove-MailboxFolderPermission 05Room:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on 05Room"}
Remove-MailboxFolderPermission 06Room:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on 06Room"}
Remove-MailboxFolderPermission Notebook01:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on Notebook01"}
Remove-MailboxFolderPermission Notebook02:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on Notebook02"}
Remove-MailboxFolderPermission Equipment01:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on Equipment01"}
Remove-MailboxFolderPermission Equipment02:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on Equipment02"}
Remove-MailboxFolderPermission Equipment03:\Calendário -User $mail -Confirm:$False -ErrorAction SilentlyContinue
if ($error[0].Exception -like "*é inválida.*"){Write-Host "No permission for $mail on  Equipment03"}

What am I doing wrong?
Using $ErrorActionPreference inside the script don’t work…
Using ErrorAction -SilentlyContinue worked once, now it shows nothing.
It’s giving me a headache.

PS: My PowerShell is in brazilian portuguese. That’s why I’m using “não encontrado.” and “é inválida.”

In a script, I would recommend to add the parameter -ErrorAction to the cmdlet that might trigger errors, rather than messing with a global variable like $ErrorActionPreference.

If the -ErrorAction parameter works fine in your script, then you can make make sure that any error from that command will be a terminating error by adding : “-ErrorAction Stop” to the command.
Then, you can handle that error with Try/Catch .

I thought it might be because $error[0].Exception is not a string, but an object of the type : System.Management.Automation.
But I tried the below, and it worked for me :

 Get-ChildItem C:\DoesNotExist -ErrorAction SilentlyContinue
Write-Output $Error[0].exception
If ($Error[0].exception -like "*Cannot find*") {
    Write-Host "Could not find it"
}

So maybe, it’s related to the language.
In this case, you can do your error checking using $error[0].FullyQualifiedErrorId , which language independent.

You may be running into some quirky behavior with implicit remoting. That’s something that isn’t currently covered in the Error Handling book, but I should explore and document it at some point.

Mathieu Buisson,

Thanks for replying.
I was trying to use $ErrorActionPreference setting it to SilentlyContinue in the beginning of the script and setting to Continue again in the end of the script.
But it worked only testing the commands without using them on a script file.
Then when I’ve tried running using the script file it wasn’t working.
So… I’ve tried using the -ErrorAction SilentlyContinue inside each command.
Worked perfeclty the first time.
The user don’t exist anymore and I was trying to get a better error information for the person who runs the script.

The problem is that it ran correclty one time… and then… only the second part ran well.
Then… nothing shows anymore… I don’t think it’s related to a script error.
But something like Dave Wyatt said. It is a remote connection to the Exhange Online… may be…
But for me… a new person to powershell… I’m not understanding anything =/

Hi,

Trying to use the “-ErrorAction Stop” to handle the error with Try/Catch/Finally.

try
{
Remove-MailboxPermission 01Videoconferencia -User $mail -AccessRights FullAccess -Confirm:$False -ErrorAction Stop
}
catch
{
Write-Host "User doesn't exist"
}

And it writes the error to the error stream and no “User doesn’t exist” message =/
It’s still related to remote connection?

Well, depends on what you mean by “nothing happens”. If the command just isn’t producing an error, then you shouldn’t see the Write-Host output anyway. If you’re seeing error output on-screen, but the ‘catch’ block isn’t triggering, then I would suspect that implicit remoting is causing a problem here (or, possibly, a very weirdly written PowerShell advanced function.)

Sorry Dave, I was editing the text when you replied =/
I just wrote more specific information.

And I’m not using any function… I just log into the Exchange Online, and ran those commands…

Adding the image as an attachment for you to see…

Can it be the cmdlet that doesn’t support the ErrorAction parameter??
http://technet.microsoft.com/en-us/library/bb629630(v=EXCHG.80).aspx

I’ve tried this now:

try
{
Get-MsolUser -UserPrincipalName useratdomain.com -ErrorAction Stop
}
catch
{
Write-Host "Test"
}

And it shown the “Test” only. With no error on the Error Stream.
Maybe that cmdlet doesn’t support ErrorAction correctly.
The funny thing is that when I’m writing the command and type “-E” and press TAB, it autocompletes the ErrorVariable and ErrorAction parameters…

Posted this problem on the Office 365 community, and their reply was:

"Hi Vandrey,

As you may have already noticed, currently we’re using the remote PowerShell connection to manage Exchange Online, therefore, some local parameters/switches may not working properly when dealing with Exchange Online via PowerShell.

Also, as described in the following articles, only the parameters provided under the “parameters” tab in the articles are supported. Sorry for any inconvenience that may be caused by the issue.

Remove-MailboxFolderPermission: http://technet.microsoft.com/en-us/library/dd351181(v=exchg.150).aspx

Remove-MailboxPermission: http://technet.microsoft.com/en-us/library/bb125153(v=exchg.150).aspx

Since the feature is not available currently, I suggest you submit feedback to our product team about the feature via: Feedback. Any comments from our customers are welcome so as to ensure that we can continually develop/improve the Microsoft products to meet our customers’ needs. That is why feedback such as yours is always highly valued.

Thanks,

Neo Yu"

Weird since it worked once… but… anyway, already sent the feedback for Microsoft.
Thanks a lot for helping!