Hi,
I wrote the function below to add some mail addresses on a distribution group.
The problem is that running it line by line on Windows Azure Module PowerShell (connected to Exchange Online module) it works normally, and trying to run it on PowerShell ISE or inside a script… it doesn’t.
How to make this cmdlet (Add-DistributionGroupMember) stop on an error for me to handle with Try/Catch?
function Add-Mails{
Param(
[Parameter(Mandatory=$True,
ParameterSetName="List")]
[String]
$List
)
foreach ($Parent in $Parents){
$ErrorActionPreference = "Stop"
Try{
Add-DistributionGroupMember -Identity $List -Member $Parent
}
Catch{
$MailsNotAdded += $Parent
}
$ErrorActionPreference = “Continue”
}
Write-Host
Write-Host $MailsNotAdded.Count “mail(s) not add(ed) in the grupo.”
Write-Host
Write-Host “Mails not added:”
$MailsNotAdded
}