This repository has been archived by the owner on Dec 21, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgroups.psm1
366 lines (352 loc) · 12.3 KB
/
groups.psm1
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
function connect-TeamsService {
<#
.SYNOPSIS
This function is used to authenticate with the Graph API REST interface
.DESCRIPTION
The function authenticate with the Graph API Interface with the tenant name
.EXAMPLE
Get-AuthToken
Authenticates you with the Graph API interface
.NOTES
NAME: Get-AuthToken
#>
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)]$User,
[Parameter(Mandatory=$true)]$tenant
)
Write-Verbose "Checking for AzureAD module..."
$AadModule = Get-Module -Name "AzureAD" -ListAvailable
if ($AadModule -eq $null) {
write-warning "AzureAD Powershell module not installed..."
Write-Warning "Install by running 'Install-Module AzureAD' from an elevated PowerShell prompt"
Write-Warning "Script can't continue..."
exit
}
# Getting path to ActiveDirectory Assemblies
# If the module count is greater than 1 find the latest version
if($AadModule.count -gt 1){
$Latest_Version = ($AadModule | Select-Object version | Sort-Object)[-1]
$aadModule = $AadModule | Where-Object { $_.version -eq $Latest_Version.version }
# Checking if there are multiple versions of the same module found
if($AadModule.count -gt 1){
$aadModule = $AadModule | Select-Object -Unique
}
$adal = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
}
else {
$adal = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = Join-Path $AadModule.ModuleBase "Microsoft.IdentityModel.Clients.ActiveDirectory.Platform.dll"
}
[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null
#$clientId = 'cc15fd57-2c6c-4117-a88c-83b1d56b4bbe'
$clientId = 'd1ddf0e4-d672-4dae-b554-9d5bdfd93547'
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$resourceAppIdURI = "https://graph.microsoft.com"
$authority = "https://login.windows.net/$Tenant"
try {
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
# https://msdn.microsoft.com/en-us/library/azure/microsoft.identitymodel.clients.activedirectory.promptbehavior.aspx
# Change the prompt behaviour to force credentials each time: Auto, Always, Never, RefreshSession
$platformParameters = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters" -ArgumentList "Always"
$userId = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserIdentifier" -ArgumentList ($User, "OptionalDisplayableId")
#New-Object 'Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationParameters'
#$query = 'wauth=http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/password'
$authResult = $authContext.AcquireTokenAsync($resourceAppIdURI,$clientId,$redirectUri,$platformParameters,$userId).Result
# If the accesstoken is valid then create the authentication header
if($authResult.AccessToken){
# Creating header for Authorization token
$authHeader = @{
'Content-Type'='application/json'
'Authorization'="Bearer " + $authResult.AccessToken
'ExpiresOn'=$authResult.ExpiresOn
}
$global:TeamsAuthToken = $authHeader
}
else {
Write-Warning "Authorization Access Token is null, please re-run authentication..."
break
}
}
catch {
write-error $_.Exception.Message
write-error $_.Exception.ItemName
break
}
}
function get-team {
<#
.SYNOPSIS
Invoking a rest request to the Microsoft graph api to get current teams
.DESCRIPTION
Describe the function in more detail
.EXAMPLE
Give an example of how to use it
#>
[CmdletBinding()]
#[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$false)]$team
)
begin {
write-verbose "checking for teams token"
if (!($TeamsAuthToken))
{throw 'please run connect-TeamsService first'}
}
process {
write-verbose "start to invoke rest request"
$nextlink = $true
$uri = "https://graph.microsoft.com/beta/groups?$('$filter')=groupTypes/any(c:c+eq+'Unified')"
$teams = Invoke-RestMethod -Uri $uri -Headers $TeamsAuthToken -Method get
while ($nextlink -eq $true){
if ($($teams.'@odata.nextLink') -ne $null){
$results = $teams.value
$teams = Invoke-RestMethod -Uri $($teams.'@odata.nextLink')-Headers $TeamsAuthToken -Method get
}
if ($($teams.'@odata.nextLink') -eq $null){
$results = $teams.value
$teams = $null
$nextlink = $false}
write-debug "getting info $teams"
$objects += $results
}
$objects
}
}
function add-Team {
<#
.SYNOPSIS
Invoking a rest request to the Microsoft graph api to add a team
.DESCRIPTION
Describe the function in more detail
.EXAMPLE
Give an example of how to use it
#>
[CmdletBinding()]
#[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$true)]$description,
[Parameter(Mandatory=$true)]$displayname,
[Parameter(Mandatory=$true)][boolean]$mailEnabled,
[Parameter(Mandatory=$true)][boolean]$securityEnabled,
[Parameter(Mandatory=$true)]$mailNickname
)
begin {
write-verbose "checking for teams token"
if (!($TeamsAuthToken))
{throw 'please run connect-TeamsService first'}
}
process {
write-verbose "start to invoke rest request"
$uri = 'https://graph.microsoft.com/beta/groups'
$postparams = @{
description = $description
displayName = $displayname
mailEnabled = $mailEnabled
mailNickname = $mailNickname
securityEnabled = $securityEnabled
groupTypes = @("Unified")
}
Invoke-RestMethod -Uri $uri -Headers $TeamsAuthToken -Method post -Body $($postparams|convertto-json)
write-debug "added team $displayName"
}
}
function remove-Team {
<#
.SYNOPSIS
Invoking a rest request to the Microsoft graph api to add a team
.DESCRIPTION
Describe the function in more detail
.EXAMPLE
Give an example of how to use it
#>
[CmdletBinding()]
#[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$true)]$teamid
)
begin {
write-verbose "checking for teams token"
if (!($TeamsAuthToken))
{throw 'please run connect-TeamsService first'}
}
process {
write-verbose "start to invoke rest request"
$uri = "https://graph.microsoft.com/beta/groups/$teamid"
Invoke-RestMethod -Uri $uri -Headers $TeamsAuthToken -Method delete
write-debug "removing team $teamid"
}
}
function get-teammembers {
<#
.SYNOPSIS
Invoking a rest request to the Microsoft graph api to get current teammembers
.DESCRIPTION
Describe the function in more detail
.EXAMPLE
Give an example of how to use it
#>
[CmdletBinding()]
#[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$true)]$teamid
)
begin {
write-verbose "checking for teams token"
if (!($TeamsAuthToken))
{throw 'please run connect-TeamsService first'}
}
process {
write-verbose "start to invoke rest request"
$nextlink = $true
$uri = "https://graph.microsoft.com/beta/groups/$teamid/members"
$teams = Invoke-RestMethod -Uri $uri -Headers $TeamsAuthToken -Method get
while ($nextlink -eq $true){
if ($($teams.'@odata.nextLink') -ne $null){
$results = $teams.value
$teams = Invoke-RestMethod -Uri $($teams.'@odata.nextLink')-Headers $TeamsAuthToken -Method get
}
if ($($teams.'@odata.nextLink') -eq $null){
$results = $teams.value
$teams = $null
$nextlink = $false}
write-debug "getting groupmember for $teamid"
$objects += $results
}
$objects
}
}
function add-TeamMember {
<#
.SYNOPSIS
Invoking a rest request to the Microsoft graph api to add teammembers
.DESCRIPTION
Describe the function in more detail
.EXAMPLE
Give an example of how to use it
#>
[CmdletBinding()]
#[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$true)]$teamid,
[Parameter(Mandatory=$true)]$member
)
begin {
write-verbose "checking for teams token"
if (!($TeamsAuthToken))
{throw 'please run connect-TeamsService first'}
}
process {
write-verbose "start to invoke rest request"
$uri = "https://graph.microsoft.com/beta/groups/$teamid/members/" + '$ref'
$postparams = @{
'@odata.id' = "https://graph.microsoft.com/beta/users/$member"
}
Invoke-RestMethod -Uri $uri -Headers $TeamsAuthToken -Method post -Body $($postparams|convertto-json)
write-debug "adding teammember $member for $teamid"
}
}
function add-TeamOwner {
<#
.SYNOPSIS
Invoking a rest request to the Microsoft graph api to add TeamOwner
.DESCRIPTION
Describe the function in more detail
.EXAMPLE
Give an example of how to use it
#>
[CmdletBinding()]
#[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$true)]$teamid,
[Parameter(Mandatory=$true)]$member
)
begin {
write-verbose "checking for teams token"
if (!($TeamsAuthToken))
{throw 'please run connect-TeamsService first'}
}
process {
write-verbose "start to invoke rest request"
$uri = "https://graph.microsoft.com/beta/groups/$teamid/owners/" + '$ref'
$postparams = @{
'@odata.id' = "https://graph.microsoft.com/beta/users/$member"
}
Invoke-RestMethod -Uri $uri -Headers $TeamsAuthToken -Method post -Body $($postparams|convertto-json)
write-debug "adding owner $member for $teamid"
}
}
function remove-TeamMember {
<#
.SYNOPSIS
Invoking a rest request to the Microsoft graph api to remove teammembers
.DESCRIPTION
Describe the function in more detail
.EXAMPLE
Give an example of how to use it
#>
[CmdletBinding()]
#[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$true)]$teamid,
[Parameter(Mandatory=$true)]$member
)
begin {
write-verbose "checking for teams token"
if (!($TeamsAuthToken))
{throw 'please run connect-TeamsService first'}
}
process {
write-verbose "start to invoke rest request"
if ($member -like '*@*'){
Write-Debug "found UPN instead of ID, converting..."
$member = (Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/users/$member/id" -Headers $TeamsAuthToken -Method get).value
}
$uri = "https://graph.microsoft.com/beta/groups/$teamid/members/$member" + '/$ref'
Invoke-RestMethod -Uri $uri -Headers $TeamsAuthToken -Method Delete -Body $postparams
write-debug "removing teammember $member for $teamid"
}
}
function remove-TeamOwner {
<#
.SYNOPSIS
Invoking a rest request to the Microsoft graph api to remove teamOwners
.DESCRIPTION
Describe the function in more detail
.EXAMPLE
Give an example of how to use it
#>
[CmdletBinding()]
#[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='Low')]
param
(
[Parameter(Mandatory=$true)]$teamid,
[Parameter(Mandatory=$true)]$member
)
begin {
write-verbose "checking for teams token"
if (!($TeamsAuthToken))
{throw 'please run connect-TeamsService first'}
}
process {
write-verbose "start to invoke rest request"
if ($member -like '*@*'){
Write-Debug "found UPN instead of ID, converting..."
$member = (Invoke-RestMethod -Uri "https://graph.microsoft.com/beta/users/$member/id" -Headers $TeamsAuthToken -Method get).value
}
$uri = "https://graph.microsoft.com/beta/groups/$teamid/owners/$member" + '/$ref'
Invoke-RestMethod -Uri $uri -Headers $TeamsAuthToken -Method Delete -Body $postparams
write-debug "removing teamowner $member for $teamid"
}
}
Export-ModuleMember connect-TeamsService, get-team, get-teammembers, add-TeamMember, remove-teammember, add-TeamOwner, remove-TeamOwner, add-Team, remove-team