This repository was archived by the owner on Jul 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate.ps1
More file actions
218 lines (185 loc) · 9.52 KB
/
Copy pathcreate.ps1
File metadata and controls
218 lines (185 loc) · 9.52 KB
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
# 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
email = $p.Contact.Personal.Email
onlySetEmailWhenEmpty = $false
}
# Troubleshooting
# $dryRun = $false
# $account = [PSCustomObject]@{
# userPrincipalName = 'j.doe@enyoi.org'
# email = 'j.doe@enyoi.nl'
# onlySetEmailWhenEmpty = $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 Email Authentication Methods for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$getEmailAuthenticationMethodUri = $baseUri + "/beta/users/$($aRef)/authentication/emailMethods"
$getEmailAuthenticationMethodResponse = Invoke-RestMethod -Uri $getEmailAuthenticationMethodUri -Method Get -Headers $authorization -Verbose:$fals
$getEmailAuthenticationMethodResponseValue = $getEmailAuthenticationMethodResponse.value
Write-Verbose ("Current email authentication method: " + ($getEmailAuthenticationMethodResponseValue | 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 Email Authentication Method
try {
if ( ![string]::IsNullOrEmpty($account.email) ) {
# Microsoft docs: https://docs.microsoft.com/nl-nl/graph/api/emailauthenticationmethod-get?view=graph-rest-beta&tabs=http
# 3ddfcfc8-9383-446f-83cc-3ab9be4be18f for emailAddress
$emailTypeId = '3ddfcfc8-9383-446f-83cc-3ab9be4be18f'
$emailAddress = "$($account.email)"
$authenticationMethodSet = $false
if ( !([string]::IsNullOrEmpty(($getEmailAuthenticationMethodResponseValue | Out-String))) ) {
$authenticationMethodSet = $true
}
if ($authenticationMethodSet -eq $false) {
Write-Verbose "No Email Authentication set. Adding Email Method with value '$($emailAddress)' for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$addEmailAuthenticationMethodUri = $baseUri + "/beta/users/$($aRef)/authentication/emailMethods"
$body = @{
"emailAddress" = $($emailAddress)
}
$bodyJson = $body | ConvertTo-Json -Depth 10
if (-Not($dryRun -eq $True)) {
$addEmailAuthenticationMethodResponse = Invoke-RestMethod -Uri $addEmailAuthenticationMethodUri -Method Post -Headers $authorization -Body $bodyJson -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully added Email Authentication Method with value '$($emailAddress)' for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: No Email Authentication set. Adding Email Authentication Method with value '$($emailAddress)' for account with id $($aRef)"
}
}
else {
$currentEmail = ($getEmailAuthenticationMethodResponseValue | Where-Object { $_.id -eq $emailTypeId }).emailAddress
if ($account.onlySetEmailWhenEmpty -eq $true) {
Write-Warning "Email Authentication Method set to only update when empty. Since this already contains data ($currentEmail), skipped update for account with id $($aRef)"
}
else {
Write-Verbose "Updating current Email Authentication Method value '$currentEmail' to value '$($emailAddress)' for account with id $($aRef)"
$baseUri = "https://graph.microsoft.com/"
$addEmailAuthenticationMethodUri = $baseUri + "/beta/users/$($aRef)/authentication/emailMethods/$emailTypeId"
$body = @{
"emailAddress" = $($emailAddress)
}
$bodyJson = $body | ConvertTo-Json -Depth 10
if (-Not($dryRun -eq $True)) {
$addEmailAuthenticationMethodResponse = Invoke-RestMethod -Uri $addEmailAuthenticationMethodUri -Method Put -Headers $authorization -Body $bodyJson -Verbose:$false
$auditLogs.Add([PSCustomObject]@{
Action = "CreateAccount"
Message = "Successfully updated Email Authentication Method value '$currentEmail' to value '$($emailAddress)' for account with id $($aRef)"
IsError = $false
})
}
else {
Write-Warning "DryRun: Updating current Email Authentication Method value '$currentEmail' to value '$($emailAddress)' 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 Email Authentication Method with value '$($emailAddress)' 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
email = $account.email
}
}
Write-Output $result | ConvertTo-Json -Depth 10