now each time when i get a wrong spelled mailbox name a get error message and the script will carry on which is fine
The operation couldn’t be performed because object ‘finanse@contoso.com’ couldn’t be found on
‘contoso.outlook.com’.
+ CategoryInfo : NotSpecified: ( [Add-MailboxPermission], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : [Server=X,TimeStamp=01/08/2017 1
0:57:06] [FailureCategory=Cmdlet-ManagementObjectNotFoundException] E5EF89B3,Microsoft.Exchange.Management.RecipientT
asks.AddMailboxPermission
+ PSComputerName : ps.outlook.com
I manage to mute the errors but what I would like to do is catch the error and at the end of script job display an report something like
Report: finanse@contoso.com - spelled wrong please correct it
But I am struggling how to catch such error,
I will be grateful for any advice
I agree with Simon. A Try/Catch block is the way to go. However, using SilentlyContinue for the -ErrorAction parameter on your Add-MailboxPermission command will not cause the code in the Catch block to execute. You have to use the word Stop instead. You might try something like the following.