Error passing a comma separated String to Add-UnifiedGroupLinks resuling in a "Couldn't find object" error

279 Views Asked by At

I am trying to bulk-load an Office 365 Group using Add-UnifiedGroupLinks which works if only passing one value as a string but the command supports a comma separated list. However, when passing it a comma separated string it returns "Couldn't find object "[email protected],[email protected]"

I can get it to work by running only one user at a time but it is MUCH slower and based on the large number of users doesn't really suite my needs. I can take the string that I build and copy and paste from that and run the same line of code manually and it works without issue. I also tried with only 3 or 5 users at a time but same issue.

$sb = New-Object System.Text.StringBuilder
$i = 1
Import-CSV "myCSV.csv" | ForEach-Object {
    [void]$sb.Append($_.UserPrincipalName + ',')
    if($i -eq 20) {
        $sb.Length--
        Add-UnifiedGroupLinks –Identity "MyGroupName" –LinkType Members –Links $sb.ToString()
        Write-Host $sb.ToString()
        $sb.Clear()
        $i = 1
        }
        Else {$i++}
    }

Note: I have additional code to handle the remainder after the loop runs but not relevant to the issue at hand.

It returns Couldn't find object "[email protected],[email protected]". However if I manually run this line, it works:

Add-UnifiedGroupLinks –Identity "MyGroupName" –LinkType Members –Links [email protected],[email protected]

Or if I do one at a time it works:

Import-CSV "myCSV.csv" | ForEach-Object {
Add-UnifiedGroupLinks –Identity "MyGroupName" –LinkType Members –Links $_.UserPrincipalName
}

but performance is awful with the last option when doing over 20K users it is averaging about 400 an hour (one every 9 seconds or so).

1

There are 1 best solutions below

0
Danny On

Instead of using String builder, I should have used an Array of Strings and passed that in:

$strArr = New-Object System.Collections.ArrayList
Import-CSV "myCSV.csv" | ForEach-Object {
    $strArr.Add($_.UserPrincipalName) > $null
    if($strArr.Count -eq 100) {
        Add-UnifiedGroupLinks –Identity "MyGroupName" –LinkType Members –Links $strArr
        $strArr.Clear()
        }
    }
Add-UnifiedGroupLinks –Identity "MyGroupName" –LinkType Members –Links $strArr