-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ps1
163 lines (149 loc) · 5.63 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
#####################################################
# HelloID-Conn-Prov-Target-IrisIntranet-Update
#
# Version: 1.0.0.0
#####################################################
$VerbosePreference = 'Continue'
# Initialize default value's
$config = $configuration | ConvertFrom-Json
$personObj = $person | ConvertFrom-Json
$aRef = $AccountReference | ConvertFrom-Json
$pd = $personDifferences | ConvertFrom-Json
$success = $false
$auditLogs = New-Object Collections.Generic.List[PSCustomObject]
$account = [PSCustomObject]@{
ExternalId = $pd.ExternalId.New
UserName = $pd.UserName.New
GivenName = $pd.Name.GivenName.New
FamilyName = $pd.Name.FamilyName.New
FamilyNameFormatted = $pd.DisplayName.New
FamilyNamePrefix = $pd.DisplayName.FamilyNamePrefix.New
IsUserActive = $true
EmailAddress = $pd.Contact.Business.Email.New
IsEmailPrimary = $true
}
#region Helper Functions
function Resolve-HTTPError {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
$HttpErrorObj = @{
FullyQualifiedErrorId = $ErrorObject.FullyQualifiedErrorId
MyCommand = $ErrorObject.InvocationInfo.MyCommand
RequestUri = $ErrorObject.TargetObject.RequestUri
}
if ($ErrorObject.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') {
$HttpErrorObj['ErrorMessage'] = $ErrorObject.ErrorDetails.Message
} elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
$stream = $ErrorObject.Exception.Response.GetResponseStream()
$stream.Position = 0
$streamReader = New-Object System.IO.StreamReader $Stream
$errorResponse = $StreamReader.ReadToEnd()
$HttpErrorObj['ErrorMessage'] = $errorResponse
}
Write-Output "'$($HttpErrorObj.ErrorMessage)', TargetObject: '$($HttpErrorObj.RequestUri), InvocationCommand: '$($HttpErrorObj.MyCommand)"
}
}
#endregion
if (-not($dryRun -eq $true)) {
try {
Write-Verbose "Updating account '$($aRef)' for '$($personObj.DisplayName)'"
Write-Verbose 'Retrieving accessToken'
$accessToken = Get-GenericScimOAuthToken -ClientID $($config.ClientID) -ClientSecret $($config.ClientSecret)
[System.Collections.Generic.List[object]]$operations = @()
if ($account.ExternalId){
$operations.Add(
[PSCustomObject]@{
op = "Replace"
path = "externalId"
value = $account.ExternalId
}
)
}
if ($account.UserName){
$operations.Add(
[PSCustomObject]@{
op = "Replace"
path = "userName"
value = $account.UserName
}
)
}
if ($account.GivenName){
$operations.Add(
[PSCustomObject]@{
op = "Replace"
path = "name.givenName"
value = $account.GivenName
}
)
}
if ($account.FamilyName){
$operations.Add(
[PSCustomObject]@{
op = "Replace"
path = "name.familyName"
value = $account.FamilyName
}
)
}
if ($account.EmailAddress){
$operations.Add(
[PSCustomObject]@{
op = "Replace"
path = 'emails[type eq "work"].value'
value = $account.EmailAddress
}
)
}
$body = [ordered]@{
schemas = @(
"urn:ietf:params:scim:api:messages:2.0:PatchOp"
)
Operations = $operations
} | ConvertTo-Json
Write-Verbose 'Adding Authorization headers'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $($config.ApiToken)")
$splatParams = @{
Uri = "$($config.BaseUrl)/api/iris/v1/$($config.ApiID)/scim/Users/$aRef"
Headers = $headers
Body = $body
Method = 'Patch'
}
$results = Invoke-RestMethod @splatParams
if ($results.id){
$logMessage = "Account '$($aRef) for '$($personObj.DisplayName)' successfully updated"
Write-Verbose $logMessage
$success = $true
$auditLogs.Add([PSCustomObject]@{
Message = $logMessage
IsError = $False
})
}
} catch {
$ex = $PSItem
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorMessage = Resolve-HTTPError -Error $ex
$auditMessage = "Account '$($aRef)' for '$($personObj.DisplayName)' not updated. Error: $errorMessage"
} else {
$auditMessage = "Account '$($aRef)' for '$($personObj.DisplayName)' not updated. Error: $($ex.Exception.Message)"
}
$auditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
Write-Error $auditMessage
}
}
$result = [PSCustomObject]@{
Success = $success
Account = $account
AuditDetails = $auditMessage
}
Write-Output $result | ConvertTo-Json -Depth 10