-
Notifications
You must be signed in to change notification settings - Fork 18
/
ProvisionGroup-PowerShell-Azure-Function.ps1
155 lines (125 loc) · 4.35 KB
/
ProvisionGroup-PowerShell-Azure-Function.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
# ProvisionGroup-PowerShell-Azure-Function.ps1
# atwork.at, Christoph Wilfing, Toni Pohl, Martina Grom
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$GroupName = $requestBody.GroupName
$upn = $requestBody.upn
# Make email address safe...
$EMail = $GroupName.Trim().Replace(" ","-")
Write-Output "TenantID: $env:TenantID AppID: $env:AppID AppSecret: ***"
Write-Output "GroupName: $GroupName EMail: $EMail UPN: $upn"
function Initialize-Authorization {
param
(
[string]
$ResourceURL = 'https://graph.microsoft.com',
[string]
[parameter(Mandatory)]
$TenantID,
[string]
[Parameter(Mandatory)]
$ClientKey,
[string]
[Parameter(Mandatory)]
$AppID
)
$Authority = "https://login.windows.net/$TenantID/oauth2/token"
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
$EncodedKey = [System.Web.HttpUtility]::UrlEncode($ClientKey)
$body = "grant_type=client_credentials&client_id=$AppID&client_secret=$EncodedKey&resource=$ResourceUrl"
# Request a Token from the graph api
$result = Invoke-RestMethod -Method Post `
-Uri $Authority `
-ContentType 'application/x-www-form-urlencoded' `
-Body $body
$script:APIHeader = @{'Authorization' = "Bearer $($result.access_token)" }
}
# Initialize Authorization
Initialize-AUthorization -TenantID $env:TenantID -ClientKey $env:AppSecret -AppID $env:AppID
# Check if group is already existing
# https://graph.microsoft.com/v1.0/groups?$filter=startswith(mail,'Project-13')
try {
$uri = "https://graph.microsoft.com/v1.0/groups?`$filter=startswith(mail,'$EMail')"
$result = Invoke-RestMethod -Method Get `
-Uri $uri `
-ContentType 'application/json' `
-Headers $script:APIHeader `
# and save the generated Group ID
$GroupID = $result.value.id
Write-Output "GroupID: $GroupID"
} catch {
Write-Output "ERROR! $_"
}
# If $GroupID is empty...
Write-Output "[$GroupID]"
if ([bool]$GroupID)
{
$msg = "EXISTING: $EMail, $GroupID"
}
else
{
$msg = "CREATED: $EMail, $GroupID"
# Create the Office 365 group
$json = @"
{ "displayName": "$GroupName",
"mailenabled" : true,
"mailnickname":"$Email",
"securityenabled" : true,
"description": "$GroupName",
"groupTypes": ["Unified"]
}
"@
try {
$result = Invoke-RestMethod -Method Post `
-Uri "https://graph.microsoft.com/v1.0/groups" `
-ContentType 'application/json' `
-Headers $script:APIHeader `
-Body $json `
-ErrorAction Stop
# and save the generated Group ID
$GroupID = $result.id
Write-Output "GroupID: $GroupID"
} catch {
Write-Output "ERROR! $_"
}
# Get the User ID
try {
$OwnerObject = Invoke-RestMethod -Method Get `
-Uri "https://graph.microsoft.com/v1.0/users/$upn" `
-ContentType 'application/json' `
-Headers $script:APIHeader `
-ErrorAction Stop
Write-Output $OwnerObject.UserPrincipalName
} catch {
Write-Output "ERROR! $_"
}
# Add User as Owner
$json = @"
{ "@odata.id": "https://graph.microsoft.com/v1.0/users/$($OwnerObject.id)" }
"@
try {
$result = Invoke-RestMethod -Method Post `
-Uri "https://graph.microsoft.com/v1.0/groups/$GroupID/owners/`$ref" `
-ContentType 'application/json' `
-Headers $script:APIHeader `
-Body $json `
-ErrorAction Stop
Write-Output "Added owner: $GroupID"
} catch {
Write-Output "ERROR! $_"
}
# add user as Member
try {
$result = Invoke-RestMethod -Method Post `
-Uri "https://graph.microsoft.com/v1.0/groups/$GroupID/members/`$ref" `
-ContentType 'application/json' `
-Headers $script:APIHeader `
-Body $json `
-ErrorAction Stop
Write-Output "Added member: $GroupID"
} catch {
Write-Output "ERROR! $_"
}
}
# return value
Out-File -Encoding Ascii -FilePath $res -inputObject $msg
Write-Output $msg