-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM365 - Sync Teams for au2mator.ps1
311 lines (217 loc) · 10.7 KB
/
M365 - Sync Teams for au2mator.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
## Environment
[string]$LogPath = "C:\_SCOworkingDir\TFS\PS-Services\M365 - Sync Teams for au2mator"
[string]$LogfileName = "Sync Teams for au2mator"
$Modules = @("SQLserver") #$Modules = @("ActiveDirectory", "SharePointPnPPowerShellOnline")
$StagingDatabase = "au2matorHelp"
$StagingServer = "Demo01"
Function Write-au2matorLog {
[CmdletBinding()]
param
(
[ValidateSet('DEBUG', 'INFO', 'WARNING', 'ERROR')]
[string]$Type,
[string]$Text
)
# Set logging path
if (!(Test-Path -Path $logPath)) {
try {
$null = New-Item -Path $logPath -ItemType Directory
Write-Verbose ("Path: ""{0}"" was created." -f $logPath)
}
catch {
Write-Verbose ("Path: ""{0}"" couldn't be created." -f $logPath)
}
}
else {
Write-Verbose ("Path: ""{0}"" already exists." -f $logPath)
}
[string]$logFile = '{0}\{1}_{2}.log' -f $logPath, $(Get-Date -Format 'yyyyMMdd'), $LogfileName
$logEntry = '{0}: <{1}> <{2}> <{3}> {4}' -f $(Get-Date -Format dd.MM.yyyy-HH:mm:ss), $Type, $RequestId, $Service, $Text
Add-Content -Path $logFile -Value $logEntry
}
Function Sync-Table_TeamsList ($Object) {
$Object | add-member -NotePropertyName Status -NotePropertyValue "1"
# Check if Table exists
$TableName = "Teams-TeamsList"
if ((Read-SqlTableData -ServerInstance $StagingServer -DatabaseName $StagingDatabase -SchemaName 'dbo' -TableName $TableName -ErrorAction SilentlyContinue).count -eq 0) {
$Object | select-Object -Property ID, Displayname, Visibility, MailNickName, Description, Status | Write-SQLTableData -Serverinstance $StagingServer -DatabaseName $StagingDatabase -TableName "Teams-TeamsList" -SchemaName "dbo" -Force
}
$QueryCheck = "Select ID from [$TableName] where ID = '$($Object.ID)'"
$QueryUpdate = "USE [$StagingDatabase]
GO
UPDATE [dbo].[$TableName]
SET [ID] = '$($Object.ID)'
,[DisplayName] = '$($Object.DisplayName)'
,[Visibility] = '$($Object.Visibility)'
,[MailNickName] = '$($Object.MailNickName)'
,[Description] = '$($Object.Description)'
,[Status] = '$($Object.Status)'
WHERE ID = '$($Object.ID)'
GO"
if (Invoke-Sqlcmd -ServerInstance $StagingServer -Database $StagingDatabase -Query $QueryCheck) {
# Update
Invoke-Sqlcmd -ServerInstance $StagingServer -Database $StagingDatabase -Query $QueryUpdate
}
else {
#Insert
$Object | select-Object -Property ID, Displayname, Visibility, MailNickName, Description, Status | Write-SQLTableData -Serverinstance $StagingServer -DatabaseName $StagingDatabase -TableName "Teams-TeamsList" -SchemaName "dbo"
}
}
Function Sync-Table_ChannelList ($Object, $TeamID) {
$Object | add-member -NotePropertyName TeamID -NotePropertyValue $TeamID
$Object | add-member -NotePropertyName Status -NotePropertyValue "1"
# Check if Table exists
$TableName = "Teams-ChannelList"
if ((Read-SqlTableData -ServerInstance $StagingServer -DatabaseName $StagingDatabase -SchemaName 'dbo' -TableName $TableName -ErrorAction SilentlyContinue).count -eq 0) {
$Object | select-Object -Property ID, Displayname, Description, TeamID, Status | Write-SQLTableData -Serverinstance $StagingServer -DatabaseName $StagingDatabase -TableName $TableName -SchemaName "dbo" -Force
}
$QueryCheck = "Select ID from [$TableName] where ID = '$($Object.ID)'"
$QueryUpdate = "USE [$StagingDatabase]
GO
UPDATE [dbo].[$TableName]
SET [ID] = '$($Object.id)'
,[DisplayName] = '$($Object.DisplayName)'
,[Description] = '$($Object.Description)'
,[TeamID] = '$($TeamID)'
,[Status] = '$($Object.Status)'
WHERE ID = '$($Object.id)'
GO"
if (Invoke-Sqlcmd -ServerInstance $StagingServer -Database $StagingDatabase -Query $QueryCheck) {
# Update
Invoke-Sqlcmd -ServerInstance $StagingServer -Database $StagingDatabase -Query $QueryUpdate
}
else {
#Insert
$Object | select-Object -Property ID, Displayname, Description, TeamID, Status | Write-SQLTableData -Serverinstance $StagingServer -DatabaseName $StagingDatabase -TableName $TableName -SchemaName "dbo"
}
}
Function Sync-Table_MemberList ($Object, $TeamID) {
$Object | add-member -NotePropertyName TeamID -NotePropertyValue $TeamID
$Object | add-member -NotePropertyName Status -NotePropertyValue "1"
if ($Member.roles -eq "Owner")
{$Object | add-member -NotePropertyName Role -NotePropertyValue "Owner"}
else {
$Object | add-member -NotePropertyName Role -NotePropertyValue "Member"
}
# Check if Table exists
$TableName = "Teams-MemberList"
if ((Read-SqlTableData -ServerInstance $StagingServer -DatabaseName $StagingDatabase -SchemaName 'dbo' -TableName $TableName -ErrorAction SilentlyContinue).count -eq 0) {
$Object | select-Object -Property UserID, email, displayName, Role, TeamID, Status | Write-SQLTableData -Serverinstance $StagingServer -DatabaseName $StagingDatabase -TableName $TableName -SchemaName "dbo" -Force
}
$QueryCheck = "Select UserID from [$TableName] where UserID = '$($Object.UserID)'"
$QueryUpdate = "USE [$StagingDatabase]
GO
UPDATE [dbo].[$TableName]
SET [UserID] = '$($Object.UserID)'
,[email] = '$($Object.email)'
,[displayName] = '$($Object.displayName)'
,[Role] = '$($Object.Role)'
,[TeamID] = '$($TeamID)'
,[Status] = '$($Object.Status)'
WHERE UserID = '$($Object.UserID)'
GO"
if (Invoke-Sqlcmd -ServerInstance $StagingServer -Database $StagingDatabase -Query $QueryCheck) {
# Update
Invoke-Sqlcmd -ServerInstance $StagingServer -Database $StagingDatabase -Query $QueryUpdate
}
else {
#Insert
$Object | select-Object -Property UserID, email, displayName, Role, TeamID, Status | Write-SQLTableData -Serverinstance $StagingServer -DatabaseName $StagingDatabase -TableName $TableName -SchemaName "dbo" -Force
}
}
Function Reset-Deleted ($TableName) {
$QueryDeleted = "USE [$StagingDatabase]
GO
UPDATE [dbo].[$TableName]
SET [Status] = '0'
GO"
Invoke-Sqlcmd -ServerInstance $StagingServer -Database $StagingDatabase -Query $QueryDeleted
}
foreach ($Module in $Modules) {
if (Get-Module -ListAvailable -Name $Module) {
Write-au2matorLog -Type INFO -Text "Module is already installed: $Module"
}
else {
Write-au2matorLog -Type INFO -Text "Module is not installed, try simple method: $Module"
try {
Install-Module $Module -Force -Confirm:$false
Write-au2matorLog -Type INFO -Text "Module was installed the simple way: $Module"
}
catch {
Write-au2matorLog -Type INFO -Text "Module is not installed, try the advanced way: $Module"
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module $Module -Force -Confirm:$false
Write-au2matorLog -Type INFO -Text "Module was installed the advanced way: $Module"
}
catch {
Write-au2matorLog -Type ERROR -Text "could not install module: $Module"
}
}
}
Write-au2matorLog -Type INFO -Text "Import Module: $Module"
Import-module $Module
}
[string]$CredentialStorePath = "C:\_SCOworkingDir\TFS\PS-Services\CredentialStore" #see for details: https://click.au2mator.com/PSCreds/?utm_source=github&utm_medium=social&utm_campaign=M365_SyncTeams&utm_content=PS1
$GraphAPICred_File = "TeamsCreds.xml"
$GraphAPICred = Import-CliXml -Path (Get-ChildItem -Path $CredentialStorePath -Filter $GraphAPICred_File).FullName
$clientId = $GraphAPICred.clientId
$clientSecret = $GraphAPICred.clientSecret
$tenantName = $GraphAPICred.tenantName
# Make Sure Database exists
Write-au2matorLog -Type INFO -Text "Make sure Database exists"
try {
Get-SqlDatabase -Name $StagingDatabase -ServerInstance $StagingServer -ErrorAction Stop
}
catch {
$sql = "
CREATE DATABASE $StagingDatabase
"
Invoke-SqlCmd -ServerInstance $StagingServer -Query $sql
}
# create variable with SQL to execute
Write-au2matorLog -Type INFO -Text "Connect to Microsoft Teams"
try {
Write-au2matorLog -Type INFO -Text "Try to connect to Microsoft Teams"
$tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $clientId
Client_Secret = $clientSecret
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $tokenBody
$headers = @{
"Authorization" = "Bearer $($tokenResponse.access_token)"
"Content-type" = "application/json"
}
try {
$URL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"
$AllTeams = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value
Reset-Deleted -TableName "Teams-TeamsList"
Reset-Deleted -TableName "Teams-ChannelList"
Reset-Deleted -TableName "Teams-MemberList"
foreach ($Team in $AllTeams) {
Write-au2matorLog -Type INFO -Text "Working with Teams: $($Team.DisplayName))"
Sync-Table_TeamsList -Object $Team
# Take care about Channels
$URL = "https://graph.microsoft.com/v1.0/teams/$($Team.id)/channels"
$AllChannels = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value
foreach ($Channel in $AllChannels) {
Write-au2matorLog -Type INFO -Text "Working with Channel: $($Channel.DisplayName))"
Sync-Table_ChannelList -Object $Channel -TeamID $Team.ID
}
$URL = "https://graph.microsoft.com/v1.0/teams/$($Team.id)/members"
$AllMembers = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value
foreach ($Member in $AllMembers) {
Write-au2matorLog -Type INFO -Text "Working with User: $($Member.User))"
Sync-Table_MemberList -Object $Member -TeamID $Team.ID
}
}
}
catch {
}
}
catch {
Write-au2matorLog -Type ERROR -Text "Error on connecting to Microsoft Teams, Error: $Error"
}