-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ps1
462 lines (390 loc) · 23.1 KB
/
create.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# Set TLS to accept TLS, TLS 1.1 and TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$VerbosePreference = "SilentlyContinue"
$InformationPreference = "Continue"
$WarningPreference = "Continue"
#Initialize default properties
$p = $person | ConvertFrom-Json
$m = $manager | ConvertFrom-Json
$aRef = $accountReference | ConvertFrom-Json
$mRef = $managerAccountReference | ConvertFrom-Json
$success = $true # Set to true at start, because only when an error occurs it is set to false
$auditLogs = [Collections.Generic.List[PSCustomObject]]::new()
# AzureAD Application Parameters #
$config = ConvertFrom-Json $configuration
$AADtenantID = $config.AADtenantID
$AADAppId = $config.AADAppId
$AADAppSecret = $config.AADAppSecret
# Change mapping here
$account = [PSCustomObject]@{
userPrincipalName = $p.Accounts.MicrosoftAzureAD.userPrincipalName
# Phone numbers use the format "+<country code> <number>x<extension>", with extension optional.
# For example, +1 5555551234 or +1 5555551234x123 are valid. Numbers are rejected when creating/updating if they do not match the required format.
mobile = "+31 " + $p.Contact.Business.Phone.Mobile
onlySetMobileWhenEmpty = $false
alternateMobile = "+31 " + $p.Contact.Personal.Phone.Mobile
onlySetAlternateMobileWhenEmpty = $false
office = "+31 " + $p.Contact.Business.Phone.Fixed
onlySetOfficeWhenEmpty = $false
enableSMSSignInMobile = $false
}
# Troubleshooting
# $dryRun = $false
# $account = [PSCustomObject]@{
# userPrincipalName = 'j.doe@enyoi.org'
# # Phone numbers use the format "+<country code> <number>x<extension>", with extension optional.
# # For example, +1 5555551234 or +1 5555551234x123 are valid. Numbers are rejected when creating/updating if they do not match the required format.
# mobile = "+31 " + '0612345678'
# onlySetMobileWhenEmpty = $false
# alternateMobile = "+31 " + '0687654321'
# onlySetAlternateMobileWhenEmpty = $false
# office = "+31 " + '0229123456'
# onlySetOfficeWhenEmpty = $false
# enableSMSSignInMobile = $false
# }
# Get current Azure AD user and authentication methods
try {
Write-Verbose "Generating Microsoft Graph API Access Token"
$baseUri = "https://login.microsoftonline.com/"
$authUri = $baseUri + "$AADTenantID/oauth2/token"
$body = @{
grant_type = "client_credentials"
client_id = "$AADAppId"
client_secret = "$AADAppSecret"
resource = "https://graph.microsoft.com"
}
$Response = Invoke-RestMethod -Method POST -Uri $authUri -Body $body -ContentType 'application/x-www-form-urlencoded'
$accessToken = $Response.access_token
#Add the authorization header to the request
$authorization = @{
Authorization = "Bearer $accesstoken"
'Content-Type' = "application/json"
Accept = "application/json"
}
$baseGraphUri = "https://graph.microsoft.com/"
$searchUri = $baseGraphUri + "v1.0/users/$($account.userPrincipalName)"
Write-Verbose "Querying Azure AD user with UPN $($account.userPrincipalName)"
$azureUser = Invoke-RestMethod -Uri $searchUri -Method Get -Headers $authorization -Verbose:$false
if ($null -ne $azureUser.id) {
Write-Verbose "Successfully queried Azure AD user $($azureUser.userPrincipalName) ($($azureUser.id))"
# Set aRef to use for further actions
$aRef = $azureUser.id
Write-Verbose "Gathering current Phone Authentication Methods for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$getPhoneAuthenticationMethodUri = $baseUri + "v1.0/users/$($aRef)/authentication/phoneMethods"
$getPhoneAuthenticationMethodResponse = Invoke-RestMethod -Uri $getPhoneAuthenticationMethodUri -Method Get -Headers $authorization -Verbose:$false
$getPhoneAuthenticationMethodResponseValue = $getPhoneAuthenticationMethodResponse.value
Write-Verbose ("Current phone authentication method: " + ($getPhoneAuthenticationMethodResponseValue | Out-String) )
}
}
catch {
$ex = $PSItem
$verboseErrorMessage = $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
$auditErrorMessage = ($ex | ConvertFrom-Json).error.message
$success = $false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Error correlating to and updating Azure MFA settings of account with id $($aRef). Error Message: $auditErrorMessage"
IsError = $True
})
if ($auditErrorMessage -Like "*Resource '$($account.userPrincipalName)' does not exist*") {
if (-Not($dryRun -eq $True)) {
$success = $false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "No Azure AD user found with UPN $($account.userPrincipalName). Possibly deleted."
IsError = $true
})
}
else {
Write-Warning "DryRun: No Azure AD user found with UPN $($account.userPrincipalName). Possibly deleted."
}
}
else {
$success = $false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Error correlating to and updating Azure MFA settings of account with id $($aRef). Error Message: $auditErrorMessage"
IsError = $True
})
}
}
if ($null -ne $azureUser.id) {
# Set Phone Authentication Method mobile
try {
if ( ![string]::IsNullOrEmpty($account.mobile) ) {
# Microsoft docs: https://docs.microsoft.com/nl-nl/graph/api/phoneauthenticationmethod-get?view=graph-rest-beta&tabs=http
# 3179e48a-750b-4051-897c-87b9720928f7 - where phoneType is mobile.
$phoneType = "mobile"
$phoneTypeId = '3179e48a-750b-4051-897c-87b9720928f7'
$phoneNumber = "$($account.mobile)"
$authenticationMethodSet = $false
if ( !([string]::IsNullOrEmpty(($getPhoneAuthenticationMethodResponseValue | Out-String))) ) {
if ( $getPhoneAuthenticationMethodResponseValue.phoneType.contains($($phoneType)) ) {
$authenticationMethodSet = $true
}
}
if ($authenticationMethodSet -eq $false) {
Write-Verbose "No Phone Authentication set for Method $($phoneType). Adding Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$addPhoneAuthenticationMethodUri = $baseUri + "v1.0/users/$($aRef)/authentication/phoneMethods"
$body = @{
"phoneNumber" = $phoneNumber
"phoneType" = $phoneType
}
$bodyJson = $body | ConvertTo-Json -Depth 10
if (-Not($dryRun -eq $True)) {
$addPhoneAuthenticationMethodResponse = Invoke-RestMethod -Uri $addPhoneAuthenticationMethodUri -Method Post -Headers $authorization -Body $bodyJson -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully added Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: No Phone Authentication set for Method $($phoneType). Adding Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
}
}
else {
$currentPhoneNumber = ($getPhoneAuthenticationMethodResponseValue | Where-Object { $_.phoneType -eq $($phoneType) }).phoneNumber
if ($account.onlySetMobileWhenEmpty -eq $true) {
Write-Warning "Phone Authentication Method $($phoneType) set to only update when empty. Since this already contains data ($currentPhoneNumber), skipped update for account with id $($aRef)"
}
else {
Write-Verbose "Updating current Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$addPhoneAuthenticationMethodUri = $baseUri + "v1.0/users/$($aRef)/authentication/phoneMethods/$phoneTypeId"
$body = @{
"phoneNumber" = $phoneNumber
"phoneType" = $phoneType
}
$bodyJson = $body | ConvertTo-Json -Depth 10
if (-Not($dryRun -eq $True)) {
$addPhoneAuthenticationMethodResponse = Invoke-RestMethod -Uri $addPhoneAuthenticationMethodUri -Method Patch -Headers $authorization -Body $bodyJson -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully updated Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: Updating current Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
}
}
}
}
}
catch {
$ex = $PSItem
$verboseErrorMessage = $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
$auditErrorMessage = ($ex | ConvertFrom-Json).error.message
$success = $false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Error setting Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef). Error message: $($auditErrorMessage)"
IsError = $True
})
}
# Enable SMS Sign-in
try {
if ($account.enableSMSSignInMobile -eq $true) {
Write-Verbose "Enabling $($phoneType) SMS Sign-in for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$enablePhoneAuthenticationMethodUri = $baseUri + "v1.0/users/$($aRef)/authentication/phoneMethods/$phoneTypeId/enableSmsSignIn"
if (-Not($dryRun -eq $True)) {
$enablePhoneAuthenticationMethodResponse = Invoke-RestMethod -Uri $enablePhoneAuthenticationMethodUri -Method Post -Headers $authorization -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully enabled $($phoneType) SMS Sign-in for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: Enabling $($phoneType) SMS Sign-in for account with id $($aRef)"
}
}
}
catch {
$ex = $PSItem
$verboseErrorMessage = $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
$auditErrorMessage = ($ex | ConvertFrom-Json).error.message
$success = $false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Error enabling $($phoneType) SMS Sign-in for account with id $($aRef). Error message: $($auditErrorMessage)"
IsError = $True
})
}
# Set Phone Authentication Method alternateMobile
try {
if ( ![string]::IsNullOrEmpty($account.alternateMobile) ) {
# Microsoft docs: https://docs.microsoft.com/nl-nl/graph/api/phoneauthenticationmethod-get?view=graph-rest-beta&tabs=http
# b6332ec1-7057-4abe-9331-3d72feddfe41 - where phoneType is alternateMobile.
$phoneType = "alternateMobile"
$phoneTypeId = 'b6332ec1-7057-4abe-9331-3d72feddfe41'
$phoneNumber = "$($account.alternateMobile)"
$authenticationMethodSet = $false
if ( !([string]::IsNullOrEmpty(($getPhoneAuthenticationMethodResponseValue | Out-String))) ) {
if ( $getPhoneAuthenticationMethodResponseValue.phoneType.contains($($phoneType)) ) {
$authenticationMethodSet = $true
}
}
if ($authenticationMethodSet -eq $false) {
Write-Verbose "No Phone Authentication set for Method $($phoneType). Adding Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$addPhoneAuthenticationMethodUri = $baseUri + "v1.0/users/$($aRef)/authentication/phoneMethods"
$body = @{
"phoneNumber" = $phoneNumber
"phoneType" = $phoneType
}
$bodyJson = $body | ConvertTo-Json -Depth 10
if (-Not($dryRun -eq $True)) {
$addPhoneAuthenticationMethodResponse = Invoke-RestMethod -Uri $addPhoneAuthenticationMethodUri -Method Post -Headers $authorization -Body $bodyJson -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully added Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: No Phone Authentication set for Method $($phoneType). Adding Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
}
}
else {
$currentPhoneNumber = ($getPhoneAuthenticationMethodResponseValue | Where-Object { $_.phoneType -eq $($phoneType) }).phoneNumber
if ($account.onlySetAlternateMobileWhenEmpty -eq $true) {
Write-Warning "Phone Authentication Method $($phoneType) set to only update when empty. Since this already contains data ($currentPhoneNumber), skipped update for account with id $($aRef)"
}
else {
Write-Verbose "Updating current Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$addPhoneAuthenticationMethodUri = $baseUri + "v1.0/users/$($aRef)/authentication/phoneMethods/$phoneTypeId"
$body = @{
"phoneNumber" = $phoneNumber
"phoneType" = $phoneType
}
$bodyJson = $body | ConvertTo-Json -Depth 10
if (-Not($dryRun -eq $True)) {
$addPhoneAuthenticationMethodResponse = Invoke-RestMethod -Uri $addPhoneAuthenticationMethodUri -Method Patch -Headers $authorization -Body $bodyJson -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully updated Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: Updating current Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
}
}
}
}
}
catch {
$ex = $PSItem
$verboseErrorMessage = $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
$auditErrorMessage = ($ex | ConvertFrom-Json).error.message
$success = $false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Error setting Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef). Error message: $($auditErrorMessage)"
IsError = $True
})
}
# Set Phone Authentication Method office
try {
if ( ![string]::IsNullOrEmpty($account.office) ) {
# Microsoft docs: https://docs.microsoft.com/nl-nl/graph/api/phoneauthenticationmethod-get?view=graph-rest-beta&tabs=http
# e37fc753-ff3b-4958-9484-eaa9425c82bc - where phoneType is office.
$phoneType = "office"
$phoneTypeId = 'e37fc753-ff3b-4958-9484-eaa9425c82bc'
$phoneNumber = "$($account.office)"
$authenticationMethodSet = $false
if ( !([string]::IsNullOrEmpty(($getPhoneAuthenticationMethodResponseValue | Out-String))) ) {
if ( $getPhoneAuthenticationMethodResponseValue.phoneType.contains($($phoneType)) ) {
$authenticationMethodSet = $true
}
}
if ($authenticationMethodSet -eq $false) {
Write-Verbose "No Phone Authentication set for Method $($phoneType). Adding Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$addPhoneAuthenticationMethodUri = $baseUri + "v1.0/users/$($aRef)/authentication/phoneMethods"
$body = @{
"phoneNumber" = $phoneNumber
"phoneType" = $phoneType
}
$bodyJson = $body | ConvertTo-Json -Depth 10
if (-Not($dryRun -eq $True)) {
$addPhoneAuthenticationMethodResponse = Invoke-RestMethod -Uri $addPhoneAuthenticationMethodUri -Method Post -Headers $authorization -Body $bodyJson -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully added Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: No Phone Authentication set for Method $($phoneType). Adding Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef)"
}
}
else {
$currentPhoneNumber = ($getPhoneAuthenticationMethodResponseValue | Where-Object { $_.phoneType -eq $($phoneType) }).phoneNumber
if ($account.onlySetOfficeWhenEmpty -eq $true) {
Write-Warning "Phone Authentication Method $($phoneType) set to only update when empty. Since this already contains data ($currentPhoneNumber), skipped update for account with id $($aRef)"
}
else {
Write-Verbose "Updating current Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$addPhoneAuthenticationMethodUri = $baseUri + "v1.0/users/$($aRef)/authentication/phoneMethods/$phoneTypeId"
$body = @{
"phoneNumber" = $phoneNumber
"phoneType" = $phoneType
}
$bodyJson = $body | ConvertTo-Json -Depth 10
if (-Not($dryRun -eq $True)) {
$addPhoneAuthenticationMethodResponse = Invoke-RestMethod -Uri $addPhoneAuthenticationMethodUri -Method Patch -Headers $authorization -Body $bodyJson -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully updated Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: Updating current Phone Authentication Method $($phoneType) value '$currentPhoneNumber' to value '$($phoneNumber)' for account with id $($aRef)"
}
}
}
}
}
catch {
$ex = $PSItem
$verboseErrorMessage = $ex
Write-Verbose "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($verboseErrorMessage)"
$auditErrorMessage = ($ex | ConvertFrom-Json).error.message
$success = $false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Error setting Phone Authentication Method $($phoneType) with value '$($phoneNumber)' for account with id $($aRef). Error message: $($auditErrorMessage)"
IsError = $True
})
}
}
# Send results
$result = [PSCustomObject]@{
Success = $success
AccountReference = $aRef
AuditLogs = $auditLogs
Account = $account
# Optionally return data for use in other systems
ExportData = [PSCustomObject]@{
id = $azureUser.id
userPrincipalName = $azureUser.userPrincipalName
mobile = $account.mobile
alternateMobile = $account.alternateMobile
office = $account.office
enableSMSSignInMobile = $account.enableSMSSignInMobile
}
}
Write-Output $result | ConvertTo-Json -Depth 10