How to format error on script

This topic contains 3 replies, has 3 voices, and was last updated by  Simon B 2 months, 3 weeks ago.

  • Author
    Posts
  • #76342

    Obel
    Participant

    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

  • #76343

    Simon B
    Participant

    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

  • #76352

    Kevyn
    Participant

    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
      }
    }
    
  • #76357

    Simon B
    Participant

    Well spotted Kevyn. I missed the silentlycontinue

You must be logged in to reply to this topic.