Skip to content

Instantly share code, notes, and snippets.

@J-a-k-o-b
Last active April 14, 2020 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save J-a-k-o-b/a072cdd5b5a441422510784e7012a206 to your computer and use it in GitHub Desktop.
Save J-a-k-o-b/a072cdd5b5a441422510784e7012a206 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Teams Policy assignment - deploy-classteams.ps1
Jakob Schaefer
09.04.2020
.DESCRIPTION
This script was designed to deploy standardized Teams for Courses.
You have to build a CSV as a Source for this script should look like:
ClassName;Teachers;Courses
SF194;teacher1@tenantname.onmicrosoft.com,teacher2@tenantname.onmicrosoft.com;Fach1,Fach2
IK132;teacher1@tenantname.onmicrosoft.com,teacher2@tenantname.onmicrosoft.com;Fach3,Fach4
With this input a seperate Teams will be created for every Course in a Class
Every Teacher will be assigned as a Teams Owner
Students that have filled the Azure AD Department Field = Classname will be assigned to the created team as a member
PARAMETER CSV
Path to the csv that contains classes, teachers and courses
EXAMPLES
.\deploy-classteams.ps1 -csv Classes.csv
#>
Param(
[string]$CSV
)
$StudentFilterAttribute="Department"
$teamstoDeploy=Import-Csv $CSV -Delimiter ';'
Write-Host -ForegroundColor Gray -Object "ACTION: Please authenticate as an Teams Admin"
$teamsSession=Connect-MicrosoftTeams
Write-Host -ForegroundColor Gray -Object "ACTION: Please authenticate as an Azure AD Admin"
$ADSession=Connect-AzureAD
foreach ($entry in $teamstoDeploy){
Write-Host -ForegroundColor Gray -Object "INFO: Start creation of Teams for Class $($entry.ClassName) "
Write-Host -ForegroundColor Gray -Object "----------------------------------------------------------------------------------------"
$teachers=@()
$teachers=$entry.Teachers.split(",")
$courses=@()
$courses=$entry.Courses.split(",")
foreach ($cours in $courses){
try {
$courseTeam=new-team -MailNickName ($entry.ClassName+"_"+$cours) -DisplayName ($entry.ClassName+" "+$cours) -Owner $teachers[0] -Template "EDU_Class" -ErrorAction Stop
Write-Host -ForegroundColor Gray -Object "INFO: A Team with the name $($courseTeam.Displayname) was created successfully, the first owner is $($teachers[0])"
#now add other teachers as owners
try {
$i=0
do {
$i++
Add-TeamUser -GroupID $courseTeam.GroupID -User $teachers[$i] -Role Owner -ErrorAction Stop
Write-Host -ForegroundColor Gray -Object "INFO: The teacher $($teachers[$i]) has beed added to the team"
} until ($i -le $teachers.count)
}
catch {
Write-Host -ForegroundColor Red -Object "ERROR: The teacher $($teachers[$i]) couldn´t be addad as a Owner. Please add him manually"
}
#now add students
$Students = Get-AzureADUser -Filter "$StudentFilterAttribute eq '$($entry.ClassName)'"
$addedstudents=@()
foreach ($student in $students){
$addedstudents+=Add-TeamUser -GroupID $courseTeam.GroupID -User $Student.ObjectID -Role Member
}
}
catch {
Write-Host -ForegroundColor Red -Object "ERROR: The team with the name $($courseTeam.Displayname) couldn´t be created. Please check the CSV/Teams for duplicates or invalid characters"
}
}
Write-Host -ForegroundColor Gray -Object "INFO: The creation of Teams for $($entry.ClassName) is finished"
Write-Host -ForegroundColor Gray -Object "----------------------------------------------------------------------------------------"
}
#Hangup
Disconnect-MicrosoftTeams
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 1 column, instead of 5. in line 1.
ClassName;Teachers;Courses
9a2020;teacher1@tenantname.onmicrosoft.com,teacher2@tenantname.onmicrosoft.com;English,German,Spanish,
4b2020;teacher1@tenantname.onmicrosoft.com,teacher2@tenantname.onmicrosoft.com;German,Sport,Math
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment