Forum Discussion
Copying AD Groups - Capturing Non-Terminating Errors
Maybe you could do something like this:
https://blogs.technet.microsoft.com/heyscriptingguy/2014/07/09/handling-errors-the-powershell-way/
- Pat O'NeilSep 21, 2018Copper Contributor
This worked and mostly achieved what I was hoping for. My next step would be to take these error messages and return the SamAccountName for these errors rather than the full error. I added Select-Object -Property SAMAccountName at the end of the $group line so the successful group additions would show just the name because without that, it was showing the group and all of it's properties. Unfortunately, I need to make this as user friendly as possible due to the staff that will be using this script. I'm struggling to figure out how I can turn these group errors that are returning with a name, into something a little more user friendly from a text standpoint.
$memberof = Get-Aduser -Identity $Copy -Properties Memberof | Select-object -ExpandProperty memberof
foreach($group in $memberof){
$group | Add-ADGroupMember -Members $UserName -passthru -ErrorVariable GroupError | Select-Object -Property SAMAccountName
If($GroupError)
{"Failed to add $username to $group"}
}