Author Posts

August 1, 2017 at 11:50 am

Hi Guys

I have simple script which give multiply access to specific mailboxes. What I would like to do is to make it little bit smarter.

Import-Csv "C:\Script CSV\List.csv" | 

foreach-object { 

$MAILBOX = $_."MAILBOX"
$USER = $_."USER"

Add-MailboxPermission $MAILBOX -USER $USER -AccessRights FullAccess -InheritanceType All -ErrorAction SilentlyContinue

##Add-RecipientPermission $MAILBOX -TRUSTEE $USER -AccessRights SendAs -confirm:$false

##Set-Mailbox -identity $MAILBOX -grantsendonbehalfto $USER

Write-Host "Mailbox Permissions for:" $USER " set on:" $MAILBOX

}

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

August 1, 2017 at 12:09 pm

if you put the adding mailbox permissions into a try/catch block

Try{
Add-MailboxPermission $MAILBOX -USER $USER -AccessRights FullAccess -InheritanceType All -ErrorAction SilentlyContinue

##Add-RecipientPermission $MAILBOX -TRUSTEE $USER -AccessRights SendAs -confirm:$false

##Set-Mailbox -identity $MAILBOX -grantsendonbehalfto $USER

Write-Host "Mailbox Permissions for:" $USER " set on:" $MAILBOX
}
Catch{
$errors += "the following mailbox could not be set " + $MAILBOX

}

$errors | out-file c:\errors.txt

August 1, 2017 at 12:58 pm

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.

Import-Csv "C:\Script CSV\List.csv" |
ForEach-object { 
  $MAILBOX = $_."MAILBOX"
  $USER = $_."USER"

  Try
  {
    Add-MailboxPermission $MAILBOX -USER $USER -AccessRights FullAccess -InheritanceType All -ErrorAction Stop
    ##Add-RecipientPermission $MAILBOX -TRUSTEE $USER -AccessRights SendAs -confirm:$false -ErrorAction Stop
    ##Set-Mailbox -identity $MAILBOX -grantsendonbehalfto $USER -ErrorAction Stop
    Write-Output "Mailbox Permissions for:" $USER " set on:" $MAILBOX
  }
  Catch
  {
    $ErrMsg = "$(Get-Date): [ERROR]: $($_.Exception.Message)"
    $ErrMsg | Out-File -FilePath C:\AddMbxPerms-Error.txt -Append
  }
}

August 1, 2017 at 1:12 pm

Well spotted Kevyn. I missed the silentlycontinue