Add multiple Teams Members

Yêu cầu:

File students.csv với header: Email (1 cột duy nhất)

Mở powershell as administrator

Chạy lệnh sau để mở Policy Execution:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

Tạo file students.csv với 1 cột, header: Email

tạo file teams.ps1 với nội dung như sau (cần thay teamId cho phù hợp, mở get link to this team để thấy TeamID trong link)

# Install the Microsoft Teams PowerShell module if not already installed
Install-Module -Name MicrosoftTeams -Force -AllowClobber

# Connect to Microsoft Teams
Connect-MicrosoftTeams

# Prompt for the Teams link
$teamsLink = Read-Host "Enter the Microsoft Teams link"

# Extract the Group ID from the link
if ($teamsLink -match "groupId=([a-f0-9-]+)") {
    $groupId = $matches[1]
} else {
    Write-Host "Invalid link. Please ensure it contains a Group ID." -ForegroundColor Red
    exit
}

# Import the CSV file containing student emails
$students = Import-Csv -Path "students.csv"

# Add each student to the team
foreach ($student in $students) {
    Add-TeamUser -GroupId $groupId -User $student.Email
}

Trong Powershell chạy lệnh: .\teams.ps1

Hướng dẫn cho Mac OS

Cài Homebrew và Powershell cho Mac OS trước:

Install Homebrew 

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install PowerShell:

brew install --cask powershell

Tạo file teams.ps1 với nội dung:

# Function to extract Team ID from the Team link
function Get-TeamIdFromLink {
    param (
        [string]$TeamLink
    )
    # Extract the Team ID from the URL
    if ($TeamLink -match "groupId=([a-f0-9\-]+)") {
        return $matches[1]
    } else {
        throw "Invalid Microsoft Teams link. Please ensure it includes the 'groupId' parameter."
    }
}

# Prompt the user for the Microsoft Teams link
$TeamLink = Read-Host "Please enter the Microsoft Teams link"

# Extract the Team ID
try {
    $TeamId = Get-TeamIdFromLink -TeamLink $TeamLink
    Write-Output "Extracted Team ID: $TeamId"
} catch {
    Write-Error $_.Exception.Message
    exit 1
}

# Prompt the user for the path to the CSV file
$csvPath = Read-Host "Please enter the full path to your CSV file"

# Check if the CSV file exists
if (-not (Test-Path -Path $csvPath)) {
    Write-Error "The CSV file does not exist at the specified path."
    exit 1
}

# Install the MicrosoftTeams module if not already installed
if (-not (Get-Module -ListAvailable -Name MicrosoftTeams)) {
    Install-Module -Name MicrosoftTeams -Force -AllowClobber
}

# Import the MicrosoftTeams module
Import-Module MicrosoftTeams

# Connect to Microsoft Teams
Connect-MicrosoftTeams

# Import members from the CSV file
$Members = Import-Csv -Path $csvPath

# Initialize an array to store successfully added members
$SuccessList = @()

# Add each member to the Microsoft Team
foreach ($member in $Members) {
    Write-Output "Attempting to add $($member.Email) to the team..."
    try {
        Add-TeamUser -GroupId $TeamId -User $member.Email -Role Member -Verbose
        Write-Output "Successfully added $($member.Email) to the team."
        $SuccessList += $member.Email
    } catch {
        Write-Error "Failed to add $($member.Email) to the team. Error: $_"
    }
}

# Disconnect from Microsoft Teams
Disconnect-MicrosoftTeams

# Report the successfully added members
if ($SuccessList.Count -gt 0) {
    Write-Output "The following members were successfully added to the team:"
    $SuccessList | ForEach-Object { Write-Output $_ }
} else {
    Write-Output "No members were successfully added."
}

Chạy file teams.ps1 với command:

pwsh teams.ps1