Forum Discussion
Unable to select / get information from Microsoft 365 group
- Sep 10, 2022
Okay, here's the basic template along with sample output from my environment.
The key things to note are:
- The script caters to the different kinds of groups (unified, distribution and dynamic distribution);
- If you're wondering about the WindowsLiveID reference, that's because the Team commandlets use the userPrincipalName, not the PrimarySmtpAddress, and while these are often the same, they don't have to be, and I've seen many instances where poorly-configured AAD Connect environments produce users with an @onmicrosoft address instead of their organisational SMTP suffix;
- Unknown group types are skipped but you can easily extend the switch statement to include other RecipientType values (independent of whether they're group types or not);
- You can only list a team (the "Team name" column) once within the CSV file, which prevents you from providing multiple values for Group assigned to the same Team. I've done this based on your earlier answer about using multiple values for Member, where I've applied the same answer to the Group column.
The sample output shows the script output boxed in orange. It's in the same format you provided in your script.
Script template
#CSV File Path. Change this location accordingly $filePath = "D:\Data\Temp\forum.csv" #read the input file if (Test-Path -Path $filePath) { $SuccessfullyCreated = 0; Import-Csv -Path $filePath | ForEach-Object { $Arguments = @{ DisplayName = $_.'Team name'; Description = $_.'Team description'; Owner = $_.Owner; MailNickname = $_.MailNickname; Template = $_.Template; } $MembershipGroupAddress = $_.Group; #create the team with specified parameters try { $Team = New-Team @Arguments -ErrorAction:Stop; switch (($MembershipGroup = Get-EXORecipient -Identity $MembershipGroupAddress -PropertySets Minimum -ErrorAction:Stop).RecipientTypeDetails) { "GroupMailbox" { # This is our UnifiedGroup category. Get-UnifiedGroupLinks -Identity ($MembershipGroup.ExternalDirectoryObjectId) -LinkType Members -ErrorAction:Stop | Where-Object { $_.RecipientType.StartsWith("User") } | ForEach-Object { Add-TeamUser -GroupId ($Team.GroupId) -User ($_.WindowsLiveID) -ErrorAction:Stop; } break; } "MailUniversalDistributionGroup" { # This is our traditional, manually-maintained distribution list category. Get-DistributionGroupMember -Identity ($MembershipGroup.ExternalDirectoryObjectId) -ErrorAction:Stop | Where-Object { $_.RecipientType.StartsWith("User") } | ForEach-Object { Add-TeamUser -GroupId ($Team.GroupId) -User ($_.WindowsLiveID) -ErrorAction:Stop; } break; } "DynamicDistributionGroup" { # This is our dynamic distribution list category. Get-DynamicDistributionGroupMember -Identity ($MembershipGroup.ExternalDirectoryObjectId) -ErrorAction:Stop | Where-Object { $_.RecipientType.StartsWith("User") } | ForEach-Object { Add-TeamUser -GroupId ($Team.GroupId) -User ($_.WindowsLiveID) -ErrorAction:Stop; } break; } default { # If we're in here, it's a type we aren't sure on how to handle, so we should skip it. continue; } } } catch { $Error[0]; } $SuccessfullyCreated++; Write-Host "Team `"$($Team.DisplayName)`" created successfully..."; } Write-Host $SuccessfullyCreated " teams were created" -ForegroundColor Green -BackgroundColor Black; } else { throw "CSV file not found. ($filePath)"; }
Sample output
The commands and output below the orange-boxed output is only included to demonstrate that the teams were indeed created and had the nominated control group (from the "Group" column in the CSV) members added.
Hopefully this helps you get started.
Cheers,
Lain
There's not enough information for us to work with here. We can't see any samples from your XLSX and there's not enough of the overall script to read for it to make sense.
The only observation I would make at this early, speculative stage is that you probably want to make use of Get-UnifiedGroupLinks instead of Get-DistributionGroupMember.
Here's a quick example of its usage for pulling members:
Cheers,
Lain
Thank you for your Feedback your suggestion. I changed it, but it does not work.
Attached all the information.
Perhaps you could have a look again.
- LainRobertsonSep 10, 2022Silver Contributor
I have a quick question about the CSV file given it only has one row.
Are you expecting to be able to provide multiple team members (as shown below in my made-up example), or is each new team only going to have exactly one member?
Edited to ask a follow-on question now that I'm reading the script:
Member (from the CSV) isn't actually being used at all (beyond being assigned to $teamMember which is not used for anything). Did you plan on doing so?
If you're not planning on using Member then ignore my question above.
Cheers,
Lain
- LainRobertsonSep 10, 2022Silver Contributor
Looking at the error screenshot, I can't read most of it, but I can read the class type and that's enough to know what the issue is.
The error is saying you haven't provided a value for the -LinkType parameter.
If you take a look at my example, you'll see "-LinkType Members" is included, whereas if you look at your script, -Identity isn't placed before $teamGroup and -LinkType is left out altogether.
Where you have this:
Get-UnifiedGroupLinks $teamGroup |
You should have this:Get-UnifiedGroupLinks -Identity $teamGroup -LinkType Members |
But this is just a quick observation on the error and not a complete solution to what you're trying to achieve.Cheers,Lain- Nikolaus YatesSep 10, 2022Copper Contributor
THX for the feedback - I updated it and it still doesn’t work - error Message attached.
The group members of the bitconsgruppe@ should be made members of the Microsoft Teams.
- LainRobertsonSep 10, 2022Silver Contributor
That error's fine - expected, even.
Get-UnifiedGroup* commands will only work against unified groups. If you run it against a distribution list, you will get the error in your screenshot.
Here's how you can tell if it's going to work or not: look at the RecipientTypeDetails.
In my screenshot below, I've enumerated my distribution lists. Looking at the final two, you can see they're not of the GroupMailbox kind, which unified groups generally are since they default to having a mailbox created.
So, running any *UnifiedGroup* commands against these final two would be expected to fail as yours did, along with my two examples above.
In other words, you ran the Get-UnifiedGroupLinks command correctly this time, just not against a unified group.
Anyhow, that's just an interesting side topic. What I was really after was the answer you provided: that you don't care about the "Member" column from the CSV, only the "Group" column. Knowing that influences the example template I'm writing.
Cheers,
Lain