Forum Discussion

Nikolaus Yates's avatar
Nikolaus Yates
Copper Contributor
Sep 09, 2022

Unable to select / get information from Microsoft 365 group

Hi,

I want to use a script to create bulk Teams in Microsoft Teams. 

 

In order to assign the members directly to the created teams, the group should be read from Excel in the script and assigned to the team as members. This works with the script with a mail-enabled security groups but not with a Microsoft 365 group. (in bold)

 

#create the team with specified parameters
$groupID = New-Team -DisplayName $teamName -Description $teamDescription -Owner $teamOwner -MailNickname $teamMailNickname -Template $teamTemplate
Get-DistributionGroupMember $teamGroup | foreach{Add-TeamUser -GroupId $groupID.GroupId -user $_.PrimarySmtpAddress}
Write-Host "Team " $teamName " created successfully..."

Does someone have a good tip, what to change that Microsoft groups works?

 

  • LainRobertson's avatar
    LainRobertson
    Sep 10, 2022

    Nikolaus Yates 

     

    Okay, here's the basic template along with sample output from my environment.

     

    The key things to note are:

     

    1. The script caters to the different kinds of groups (unified, distribution and dynamic distribution);
    2. 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;
    3. 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);
    4. 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

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    Nikolaus Yates 

     

    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

    • Nikolaus Yates's avatar
      Nikolaus Yates
      Copper Contributor

      LainRobertson 

       

      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. 

       

       

      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        Nikolaus Yates 

         

        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

Resources