# How to format error on script

**URL:** <https://forums.powershell.org/t/how-to-format-error-on-script/9103>\
**Category:** PowerShell Help\
**Created:** [August 1, 2017, 6:20am UTC](https://forums.powershell.org/t/how-to-format-error-on-script/9103 "2017-08-01T06:20:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![obel](https://avatars.discourse-cdn.com/v4/letter/o/ce7236/32.png) [@obel](https://forums.powershell.org/u/obel)\
**Post date:** [August 1, 2017, 6:20am UTC](https://forums.powershell.org/t/how-to-format-error-on-script/9103/1 "2017-08-01T06:20:46Z")

</div>

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](http://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](http://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](mailto:finanse@contoso.com) - spelled wrong please correct it

But I am struggling how to catch such error,  
I will be grateful for any advice

---

<div class="post-metadata">

**Author:** ![simbrook](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@simbrook](https://forums.powershell.org/u/simbrook)\
**Post date:** [August 1, 2017, 6:39am UTC](https://forums.powershell.org/t/how-to-format-error-on-script/9103/2 "2017-08-01T06:39:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![miracles1315](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@miracles1315](https://forums.powershell.org/u/miracles1315)\
**Post date:** [August 1, 2017, 7:28am UTC](https://forums.powershell.org/t/how-to-format-error-on-script/9103/3 "2017-08-01T07:28:04Z")

</div>

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
  }
}
```

---

<div class="post-metadata">

**Author:** ![simbrook](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@simbrook](https://forums.powershell.org/u/simbrook)\
**Post date:** [August 1, 2017, 7:42am UTC](https://forums.powershell.org/t/how-to-format-error-on-script/9103/4 "2017-08-01T07:42:42Z")

</div>

Well spotted Kevyn. I missed the silentlycontinue

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/how-to-format-error-on-script/9103/5 "2024-05-16T20:38:23Z")

</div>

Closed on May 16, 2024, 8:38pm UTC


