-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add-UsersToTeamsFromCSVFile.ps1
370 lines (292 loc) · 10.7 KB
/
Add-UsersToTeamsFromCSVFile.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<#PSScriptInfo
.VERSION 1.7
.GUID 026e9227-935f-4717-8eea-97813f59400c
.AUTHOR Walter van Heuven
.COMPANYNAME
.COPYRIGHT
.TAGS Microsoft Teams, Teams, Private Channel, Import, CSV
.LICENSEURI
.PROJECTURI https://github.com/waltervanheuven/Add-UsersToTeamsFromCSVFile
.ICONURI
.EXTERNALMODULEDEPENDENCIES
MicrosoftTeams
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
28 April 2021: 1.7 Changed script so that it does not exit when email address is unknown
02 February 2021: 1.6 Fixed issue when adding newly added user to private channel
20 January 2021: 1.5 Bug fixes and changed requirements to MicrosoftTeams 1.1.10-preview
16 October 2020: 1.4
09 October 2020: 1.3
08 October 2020: 1.2
08 October 2020: 1.1
06 October 2020: 1.0 First release on PowerShell Gallery
27 August 2020: Development of this script started with a script written by Jan Derrfuss
.PRIVATEDATA
#>
<#
.SYNOPSIS
Script to add users to teams or to private team channels using a CSV file.
.DESCRIPTION
Script to add users to teams or to private team channels using a CSV file. Users are added using their email address.
Requires MicrosoftTeams 1.1.10-preview.
Install-Module -Name MicrosoftTeams -RequiredVersion 1.1.10-preview -AllowPrerelease
Run Connect-MicrosoftTeams to connect to the AzureCloud before running this script.
You can only add users if you are the owner of the team.
To add users to a teams, the CSV file requires 2 columns, separated by a comma.
First line of CSV file is a header indicating the column names: email, team
To add users to private channels in a team, the CSV file requires 3 columns, separated by commas.
First line of CSV file is a header indicating the column names: email, team, privatechannel
An additional column in the CSV file 'role' is optional, default role of user added is Member.
The team column should indate the name of the team and the privatechannel should indicate
the name of the private channel. Note that when a user is not a member of the team it will
be added to the team first before it is added to the private channel.
Script reports start and end time as well as the number of users added to teams and channels.
.PARAMETER CSVFileToProcess
Name of the CSV file with at least two columns: email, team
.EXAMPLE
Add-UsersToTeamsFromCSVFile.ps1 -CSVFileToProcess .\students.csv
students.csv
email, team
[email protected], Module1 Team
[email protected], Module1 Team
[email protected], Module2 Team
[email protected], Module2 Team
Script adds two students to two teams.
Please note that the team name should be surrounded by quotes when the name contains a comma or quotes.
.EXAMPLE
Add-UsersToTeamsFromCSVFile.ps1 .\students.csv
-CSVFileToProcess is optional.
Content of file: students.csv
email, team
[email protected], Module1 Team
[email protected], Module1 Team
Script adds two students to the team: Module1 Team.
.EXAMPLE
Add-UsersToTeamsFromCSVFile.ps1 .\students.csv
Content of file: students.csv
email, team, privatechannel
[email protected], Module1 Team, Lab1
[email protected], Module1 Team, Lab2
Script adds two students to private channels within the same team.
.EXAMPLE
Add-UsersToTeamsFromCSVFile.ps1 .\users.csv
Content of file: users.csv
email, team, privatechannel, role
[email protected], "Module's Team", Lab1, Member
[email protected], "Module's Team", Lab1, Member
Script adds two users to private channels with the same team. Column 'role' indicates their role.
.EXAMPLE
Add-UsersToTeamsFromCSVFile.ps1 .\users.csv -Debug
Script provides also debug information. Useful when errors occur.
#>
#Requires -Module @{ModuleName = 'MicrosoftTeams'; RequiredVersion = '1.1.10'}
Param (
[Parameter(ParameterSetName = "Inputparameter", Position = 0, HelpMessage="Input CSV file: ", Mandatory = $true)]
[String] $CSVFileToProcess
)
# BEGIN
$startTime = Get-Date -DisplayHint Date
# Check if input file is provided
if ([string]::IsNullOrEmpty($CSVFileToProcess)) {
Write-Error "Input CSV file missing"
EXIT
}
# Check if file exists and has required columns
if (Test-Path -LiteralPath $CSVFileToProcess -PathType Leaf) {
Write-Output "Processing CSV file: $CSVFileToProcess"
$inputCsvFile = Import-Csv -Path $CSVFileToProcess
# check CSV file
$colCount = ($inputCsvFile | get-member -type NoteProperty).Count
if ($colCount -lt 2) {
Write-Error "Error in file: '$CSVFileToProcess', at least 2 columns needed, found: $colCount"
Write-Error "Required columns in the CSV file: email, team"
Write-Error "Additional column needed when adding students to channel: privatechannel"
Write-Error "Optional column: role, if missing, role is Member"
EXIT
}
#
$n = 2
$addedTeamMembers = 0
$addedChannelMembers = 0
$currentTeam = ""
[System.Collections.ArrayList] $currentTeamMembers = @()
$currentChannel = ""
[System.Collections.ArrayList] $currentChannelMembers = @()
$changedTeam = 0
$theEmail = ""
$theTeam = ""
$theChannel = ""
$theRole = ""
#
foreach ($line in $inputCsvFile) {
if ([string]::IsNullOrEmpty($line.email)) {
Write-Error "Line $n, column 'email' is empty or column 'email' is missing"
EXIT
} else {
try {
$null = [mailaddress]$line.email
$theEmail = $line.email
}
catch {
Write-Error "Line $n, email: '$($line.email)' not in not a valid email address"
EXIT
}
}
if ([string]::IsNullOrEmpty($line.team)) {
Write-Error "Line $n, column 'team' is empty or column 'team' missing"
EXIT
} else {
$theTeam = $line.team
}
# check if Role column is available, if not set Role to Member
if ([string]::IsNullOrEmpty($line.role)) {
$theRole = "Member"
} else {
$theRole = $line.role
}
if ([string]::IsNullOrEmpty($line.privatechannel)) {
$theChannel = ""
} else {
$theChannel = $line.privatechannel
}
# Team
if ($currentTeam -ne $theTeam) {
try {
Write-Output "Obtaining GroupID and current members from team... (this can take some time)"
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Debug "Obtaining GroupID from team: $theTeam"
}
$grpid = (Get-Team -DisplayName $theTeam) | Select-Object -ExpandProperty GroupId
if ($null -eq $grpid) {
Write-Error "Line $n, Get-Team: GroupID of '$theTeam' is null"
EXIT
}
[System.Collections.ArrayList] $currentTeamMembers = @()
$list = (Get-TeamUser -GroupId $grpid) | Select-Object -ExpandProperty User
foreach ($m in $list) {
[void] $currentTeamMembers.Add($m)
}
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Debug "Current members:"
$currentTeamMembers | Write-Debug
}
$currentTeam = $theTeam
$changedTeam = 1
}
catch {
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Error $_.Exception.Message
}
Write-Error "Line $n, Get-Team: Unable to connect to MicrosoftTeams (AzureCloud)"
Write-Error "Before running this script enter: Connect-MicrosoftTeams"
EXIT
}
} else {
$changedTeam = 0
}
if ($currentTeamMembers -Contains $theEmail) {
# do not warn when adding people to a private channel within a team
if ($theChannel -eq "") {
Write-Warning "Line $n, $theEmail already member of team: $theTeam"
}
} else {
try {
$oldN = ((Get-TeamUser -GroupId $grpid) | Select-Object -ExpandProperty User).count
Add-TeamUser -GroupId $grpid -User $theEmail -Role $theRole
Write-Output "Line $n, added: '$theEmail', role: '$theRole' to team: '$theTeam'"
$addedTeamMembers += 1
[void] $currentTeamMembers.Add($theEmail)
if ($theChannel -ne "") {
# sleep 5 seconds to allow AzureCloud update
Start-Sleep -s 5
# make sure newly added user is indeed added, if not wait further until AzureCloud updates
$newN = ((Get-TeamUser -GroupId $grpid) | Select-Object -ExpandProperty User).count
$waitCnt = 0
while ($newN -le $oldN) {
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Output "Sleep..."
}
Start-Sleep -s 1
$newN = ((Get-TeamUser -GroupId $grpid) | Select-Object -ExpandProperty User).count
$waitCnt += 1
if ($waitCnt -gt 30) {
Write-Debug "Timeout Error. Unable to add user. Please try again."
EXIT
}
}
}
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Debug "Members:"
$currentTeamMembers | Write-Debug
}
}
catch {
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Error $_.Exception.Message
}
Write-Error "Line $n, Error when executing Add-TeamUser. Unknown email address: '$theEmail' (not a user)"
#EXIT
}
}
# Channel
if ($theChannel -ne "") {
if (($currentChannel -ne $theChannel) -or ($changedTeam -eq 1)) {
try {
[System.Collections.ArrayList] $currentChannelMembers = @()
$list = (Get-TeamChannelUser -GroupId $grpid -DisplayName $theChannel) | Select-Object -ExpandProperty User
foreach ($m in $list) {
[void] $currentChannelMembers.Add($m)
}
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Debug "Current private channel member:"
$currentChannelMembers | Write-Debug
}
}
catch {
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Error $_.Exception.Message
}
Write-Error "Line $n, Error when executing Get-TeamChannelUser"
EXIT
}
$currentChannel = $theChannel
}
if ($currentChannelMembers -Contains $theEmail) {
Write-Warning "Line $n, $theEmail already member of private channel: $theChannel"
} else {
try {
if ($theRole -eq "Owner") {
Add-TeamChannelUser -GroupId $grpid -DisplayName $theChannel -User $theEmail -Role $theRole
} else {
Add-TeamChannelUser -GroupId $grpid -DisplayName $theChannel -User $theEmail
}
Write-Output "Line $n, added: '$theEmail', to private channel: '$theChannel' in team: '$theTeam'"
$addedChannelMembers += 1
[void] $currentChannelMembers.Add($theEmail)
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Debug "Private channel members:"
$currentChannelMembers | Write-Debug
}
}
catch {
if ($PSBoundParameters.ContainsKey('Debug')) {
Write-Error $_.Exception.Message
}
Write-Error "Line $n, Error when executing Add-TeamChannelUser, user: '$theEmail', channel: '$theChannel'"
EXIT
}
}
}
$n += 1
}
$endTime = Get-Date -DisplayHint Date
Write-Output "Finished processing file: '$CSVFileToProcess'"
Write-Output "Start time: $startTime"
Write-Output "End time: $endTime"
Write-Output "Total number of people added to teams: $addedTeamMembers"
Write-Output "Total number of people added to private channels: $addedChannelMembers"
} else {
Write-Error "Input file: '$CSVFileToProcess' does not exist"
EXIT
}