-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.ps1
160 lines (142 loc) · 4.25 KB
/
functions.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
$Global:Config = Get-Content config.json | ConvertFrom-Json
function New-Config {
Param(
[Parameter(Mandatory = $true)]
[string]$ApplicationID,
[Parameter(Mandatory = $true)]
[string]$Secret,
[Parameter(Mandatory = $true)]
[string]$TenantName
)
$Object = [PSCustomObject]@{
ApplicationID = $ApplicationID
Secret = $Secret
TenantName = $TenantName
}
$Object | ConvertTo-Json | Out-File config.json
}
function Get-ApplicationAuthToken {
Param(
[Parameter(Mandatory = $true)]
[string]$ApplicationID,
[Parameter(Mandatory = $true)]
[string]$Secret,
[Parameter(Mandatory = $true)]
[string]$TenantName
)
$TokenEndpoint = "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token"
$requestBody = @{
client_id = $ApplicationID
scope = "https://graph.microsoft.com/.default"
client_secret = $Secret
grant_type = "client_credentials"
}
$tokenRequest = Invoke-RestMethod -Uri $TokenEndpoint -Method Post -Body $requestBody
if ($tokenRequest.access_token) {
$authToken = @{
'Content-Type' = 'application/json'
'Authorization' = "Bearer " + $tokenRequest.access_token
'ExpiresOn' = (Get-Date).AddSeconds($tokenRequest.expires_in).ToUniversalTime()
}
return $authToken
}
else {
throw "Failed to get authToken"
}
}
function Test-AuthToken {
Param(
$authToken
)
if (!($authToken)) {
$authToken = Get-AuthToken
}
else {
$DateTime = (Get-Date).ToUniversalTime()
$TokenExpires = ($authToken.ExpiresOn - $DateTime).Minutes
if ($TokenExpires -le 1) {
$authToken = Get-AuthToken
}
}
return $authToken
}
function Get-GraphData {
Param (
$uri,
$authToken
)
$Response = (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get)
$Output = $Response.Value
$NextLink = $Response."@odata.nextLink"
while ($NextLink -ne $null) {
$Response = (Invoke-RestMethod -Uri $NextLink -Headers $authToken -Method Get)
$NextLink = $Response."@odata.nextLink"
$Output += $Response.Value
}
if (!($Output)) {
$Output = $Response
}
return $Output
}
function Get-AADUserFromMail {
param(
$mail,
$authToken
)
$user = Get-GraphData -uri "https://graph.microsoft.com/beta/users?`$filter=mail eq '$mail'" -authToken $authToken
return $user
}
function Get-TeamOwners {
param(
$aadGroupID,
$authToken
)
$owners = Get-GraphData -uri "https://graph.microsoft.com/beta/groups/$aadGroupID/owners" -authToken $authToken
return $owners
}
function Get-TeamMembers {
param(
$aadGroupID,
$authToken
)
$users = Get-GraphData -uri "https://graph.microsoft.com/beta/groups/$aadGroupID/members" -authToken $authToken
return $users
}
function Add-TeamMember {
param(
$userId,
$groupId,
$authToken
)
$body = @{"@odata.id" = "https://graph.microsoft.com/beta/directoryObjects/$userId" } | ConvertTo-Json
$result = Invoke-RestMethod -Method Post -Uri "https://graph.microsoft.com/beta/groups/$groupId/members/`$ref" -Body $body -Headers $authToken
return $result
}
function Add-TeamOwner {
param(
$userId,
$groupId,
$authToken
)
$body = @{"@odata.id" = "https://graph.microsoft.com/beta/directoryObjects/$userId" } | ConvertTo-Json
$result = Invoke-RestMethod -Method Post -Uri "https://graph.microsoft.com/beta/groups/$groupId/owners/`$ref" -Body $body -Headers $authToken
return $result
}
function Remove-TeamMember {
param(
$userId,
$groupId,
$authToken
)
$result = Invoke-RestMethod -Method Delete -Uri "https://graph.microsoft.com/beta/groups/$groupId/members/$userId/`$ref" -Headers $authToken
return $result
}
function Remove-TeamOwner {
param(
$userId,
$groupId,
$authToken
)
$result = Invoke-RestMethod -Method Delete -Uri "https://graph.microsoft.com/beta/groups/$groupId/owners/$userId/`$ref" -Headers $authToken
return $result
}