I am using the following code to bulk add members to a distribution list. The try\catch is not trapping the error, although I am able to see the error in the console when I debug the program. It just continues processing the code even if I use "-ErrorAction Stop".
foreach ($item in $users)
{
try
{
Add-DistributionGroupMember -Identity $TargetDL -Member $item -erroraction stop
}
catch
{
[System.Windows.Forms.MessageBox]::Show($PSItem, 'Error')
}
}
Try\Catch not trapping error
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- Shelltastic
- Posts: 65
- Last visit: Mon Feb 19, 2024 11:31 am
Re: Try\Catch not trapping error
For this specific error, its a duplicate error. However, I would assume when any error is thrown the behavior would be the same since I am already specifying -ErrorAction stop and it's not working. Screen shot below..
https://1drv.ms/u/s!AgBAbImA7NSlsxyQnBdWjslyeHA8
https://1drv.ms/u/s!AgBAbImA7NSlsxyQnBdWjslyeHA8
- Shelltastic
- Posts: 65
- Last visit: Mon Feb 19, 2024 11:31 am
Re: Try\Catch not trapping error
Ok, I did already read that post in my searches this morning. Looks like we came across the same one. I'll have to find another way to go about this then, thanks.
Re: Try\Catch not trapping error
You need to start by not sending bad user ids. They must be the correct type and must exist. The strings cannot have odd characters or spaces.
You can only use these types of names:
•Alias
•Distinguished name (DN)
•Canonical DN
•Email address
•GUID
You can only use these types of names:
•Alias
•Distinguished name (DN)
•Canonical DN
•Email address
•GUID