-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ps1
295 lines (250 loc) · 11.4 KB
/
update.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
#################################################
# HelloID-Conn-Prov-Target-SSRPM-AutoEnroll-Update
# PowerShell V2
#################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
#region functions
function get-SSRPMuserById {
[CmdletBinding()]
param (
[Parameter(Mandatory)][String]$Id
, [Parameter(Mandatory)][string]$ConnectionString
)
try {
Write-Information "Select user from [enrolled users] with id $Id"
$query = "SELECT * FROM [enrolled users] WHERE id = '$($Id)'"
# Initialize connection and query information
# Connect to the SQL server
$SqlConnection = [System.Data.SqlClient.SqlConnection]::new($ConnectionString)
$SqlCmd = [System.Data.SqlClient.SqlCommand]::new($query, $SqlConnection)
$SqlAdapter = [System.Data.SqlClient.SqlDataAdapter]::new($SqlCmd)
$DataSet = [System.Data.DataSet]::new()
$SqlAdapter.Fill($DataSet) | out-null
$sqlData = $DataSet.Tables[0]
Write-Information "Found $($sqlData.Rows.Count) user(s) in SSRPM database"
$Rowlist = [System.Collections.Generic.List[object]]::new()
foreach ($Row in $sqlData.rows) {
$Rowlist.Add(($row | Select-Object -Property * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors))
}
return , $Rowlist
}
catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
function get-SSRPMAnswers {
[CmdletBinding()]
param (
[Parameter(Mandatory)][String]$UserId
, [Parameter(Mandatory)][string]$ConnectionString
)
try {
Write-Information "Select user from [enrolled users] with id $Id"
#$query = "SELECT * FROM [User Answers] WHERE [Account id] = '$($UserId)'"
$query = "SELECT questionID,text=Answer FROM [User Answers]ua inner join [Questions]q on q.Question=ua.Question WHERE [Account id] = '$($UserId)'"
# Initialize connection and query information
# Connect to the SQL server
$SqlConnection = [System.Data.SqlClient.SqlConnection]::new($ConnectionString)
$SqlCmd = [System.Data.SqlClient.SqlCommand]::new($query, $SqlConnection)
$SqlAdapter = [System.Data.SqlClient.SqlDataAdapter]::new($SqlCmd)
$DataSet = [System.Data.DataSet]::new()
$SqlAdapter.Fill($DataSet) | out-null
$sqlData = $DataSet.Tables[0]
$Rowlist = [System.Collections.Generic.List[object]]::new()
foreach ($Row in $sqlData.rows) {
$Rowlist.Add(($row | Select-Object -Property * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors))
}
return , $Rowlist
}
catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
function Compare-Answers {
[CmdletBinding()]
param(
[PSCustomObject]
$OldAnswers,
[PSCustomObject]
$NewAnswers
)
if($null -eq $OldAnswers)
{
return $true
}
$oldAnswersGrouped = $OldAnswers | Group-Object -property "QuestionId" -AsHashTable
$ReturnValue = $false
foreach ($question in $NewAnswers) {
$oldQuestion = $oldAnswersGrouped[$Question.QuestionID]
if (($null -eq $oldQuestion) -or ($question.text -ne $oldQuestion.text)) {
$ReturnValue = $true
break
}
}
write-output $Returnvalue
}
function Update-SSRPMuser {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][string]$connectionString,
[Parameter(Mandatory = $true)][Object]$account
)
try {
if ([string]::IsNullOrEmpty($account.sAMAccountName) -OR
[string]::IsNullOrEmpty($account.CanonicalName) -OR
[string]::IsNullOrEmpty($account.ObjectSID)) {
Throw "one of the mandatory fields is empty or missing"
}
$XML_Answers = $null
$AnswerList = $Account.answers
foreach ($answer in $AnswerList) {
if (-NOT([string]::IsNullOrEmpty($answer.QuestionID) -OR [string]::IsNullOrEmpty($answer.text))) {
$XML_Answers += "<a id=""$($answer.QuestionID)"">$($answer.text)</a>"
}
}
$XML_Answers = "<answers>" + $XML_Answers + "</answers>"
#SQL connection
$SqlConnection = [System.Data.SqlClient.SqlConnection]::new($ConnectionString)
$SqlCmd = [System.Data.SqlClient.SqlCommand]::new()
#sql command
$SqlCmd.CommandText = "updateUser"
$SqlCmd.CommandType = [System.Data.CommandType]::StoredProcedure
#sql parameters
[void]$SqlCmd.Parameters.AddWithValue("@SSRPM_ID", $account.Id)
[void]$SqlCmd.Parameters.AddWithValue("@AD_CanonicalName", $account.CanonicalName)
[void]$SqlCmd.Parameters.AddWithValue("@AD_sAMAccountName", $account.SamAccountName)
[void]$SqlCmd.Parameters.AddWithValue("@AD_EmailAddress", $account.mail)
[void]$SqlCmd.Parameters.AddWithValue("@AD_ObjectSID", $account.ObjectSID)
[void]$SqlCmd.Parameters.AddWithValue("@Private_EmailAddress", $account.PrivateMail)
[void]$SqlCmd.Parameters.AddWithValue("@Private_Mobile", $account.PrivateMobile)
[void]$SqlCmd.Parameters.AddWithValue("@XML_Answers", $XML_Answers)
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = [System.Data.SqlClient.SqlDataAdapter]::new($SqlCmd)
$DataSet = [System.Data.DataSet]::new()
#execute
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
}
catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
function ConvertTo-AccountObject {
param(
[PSCustomObject]
$SourceUserObject,
[System.Collections.Generic.List[object]]
$AnswersqueryResult
)
$AccountObject = [PSCustomObject]@{
CanonicalName = $SourceUserObject.'Account Name'
SAMAccountName = $SourceUserObject.sAMAccountName
Mail = $SourceUserObject.InternalEmailAddress
ObjectSid = $SourceUserObject.'Account SID'
PrivateMail = $SourceUserObject.'Email Address'
PrivateMobile = $SourceUserObject.'Mobile Phone Number'
Answers = $AnswersqueryResult | ConvertTo-Json | ConvertFrom-Json
}
Write-Output $AccountObject
}
#endregion
try {
# Verify if [aRef] has a value
if ([string]::IsNullOrEmpty($($actionContext.References.Account))) {
throw 'The account reference could not be found'
}
Write-Information 'Verifying if a SSRPM-AutoEnroll account exists'
$SSRPMUser = get-SSRPMuserById -Id $actionContext.References.Account -ConnectionString $actionContext.Configuration.ConnectionString
# Always compare the account against the current account in target system
if ($null -ne $SSRPMUser) {
$SSRPMAnswers = get-SSRPMAnswers -UserId $actionContext.References.Account -ConnectionString $actionContext.Configuration.ConnectionString
$correlatedAccount = ConvertTo-AccountObject -SourceUserObject $SSRPMUser[0] -Answers $SSRPMAnswers
$outputContext.PreviousData = $correlatedAccount
if ($null -ne $actionContext.Data.ProfileId) {
$actionContext.Data.PSObject.Properties.Remove("profileId")
}
$actionContext.Data.Answers = $actionContext.Data.Answers | ConvertFrom-Json
$sidObjectBytes = [system.convert]::FromBase64String($actionContext.Data.Objectsid)
$securityIdentifier = [system.security.Principal.SecurityIdentifier]::new($sidObjectBytes,0)
$SidString = $securityIdentifier.ToString()
$actionContext.Data.Objectsid = $SidString
$splatCompareProperties = @{
ReferenceObject = @($correlatedAccount.PSObject.Properties)
DifferenceObject = @($actionContext.Data.PSObject.Properties)
}
$propertiesChanged = Compare-Object @splatCompareProperties -PassThru | Where-Object { $_.SideIndicator -eq '=>' }
$answersChanged = Compare-Answers -OldAnswers $correlatedAccount.Answers -NewAnswers $actionContext.Data.Answers
if ($propertiesChanged -or $answersChanged) {
$action = 'UpdateAccount'
}
else {
$action = 'NoChanges'
}
}
else {
$action = 'NotFound'
}
# Process
switch ($action) {
'UpdateAccount' {
if ($answersChanged) {
Write-Information "Account property(s) required to update: $($propertiesChanged.Name -join ', '), Answers"
}
else {
Write-Information "Account property(s) required to update: $($propertiesChanged.Name -join ', ')"
}
# Make sure to test with special characters and if needed; add utf8 encoding.
if (-not($actionContext.DryRun -eq $true)) {
Write-Information "Updating SSRPM-AutoEnroll account with accountReference: [$($actionContext.References.Account)]"
$actionContext.Data | Add-Member -MemberType NoteProperty -Name "id" -Value $($actionContext.References.Account)
$result = Update-SSRPMuser -connectionString $actionContext.Configuration.ConnectionString -account $actionContext.Data
}
else {
Write-Information "[DryRun] Update SSRPM-AutoEnroll account with accountReference: [$($actionContext.References.Account)], will be executed during enforcement"
}
$outputContext.Success = $true
if ($answersChanged) {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Update account was successful, Account property(s) updated: [$($propertiesChanged.name -join ','), Answers]"
IsError = $false
})
}
else {
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Update account was successful, Account property(s) updated: [$($propertiesChanged.name -join ',')]"
IsError = $false
})
}
break
}
'NoChanges' {
Write-Information "No changes to SSRPM-AutoEnroll account with accountReference: [$($actionContext.References.Account)]"
$outputContext.Success = $true
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = 'No changes will be made to the account during enforcement'
IsError = $false
})
break
}
'NotFound' {
Write-Information "SSRPM-AutoEnroll account: [$($actionContext.References.Account)] could not be found, possibly indicating that it could be deleted"
$outputContext.Success = $false
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "SSRPM-AutoEnroll account with accountReference: [$($actionContext.References.Account)] could not be found, possibly indicating that it could be deleted"
IsError = $true
})
break
}
}
}
catch {
$outputContext.Success = $false
$ex = $PSItem
$auditMessage = "Could not update SSRPM-AutoEnroll account. Error: $($ex.Exception.Message)"
Write-Warning "Error at Line '$($ex.InvocationInfo.ScriptLineNumber)': $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
}