-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ps1
153 lines (137 loc) · 5.29 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
##########################################################
# HelloID-Conn-Prov-Target-Spacewell-Axxerion-Update
#
# Version: 1.0.0.0
##########################################################
$VerbosePreference = "Continue"
# Initialize default value's
$c = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json
$success = $false
$auditLogs = New-Object Collections.Generic.List[PSCustomObject]
$prefix = ""
if(-Not([string]::IsNullOrEmpty($p.Name.FamilyNamePrefix)))
{
$prefix = $p.Name.FamilyNamePrefix + " "
}
$partnerprefix = ""
if(-Not([string]::IsNullOrEmpty($p.Name.FamilyNamePartnerPrefix)))
{
$partnerprefix = $p.Name.FamilyNamePartnerPrefix + " "
}
switch($p.Name.Convention)
{
"B" {$surname += $p.Name.FamilyName; $AxPrefix = $prefix}
"P" {$surname += $p.Name.FamilyNamePartner; $AxPrefix = $partnerprefix}
"BP" {$surname += $p.Name.FamilyName + " - " + $partnerprefix + $p.Name.FamilyNamePartner; $AxPrefix = $prefix}
"PB" {$surname += $p.Name.FamilyNamePartner + " - " + $prefix + $p.Name.FamilyName ; $AxPrefix = $partnerprefix}
default {$surname += $p.Name.FamilyName; $AxPrefix = $prefix}
}
$email = $p.Accounts.MicrosoftActiveDirectory.Mail
$account = [PSCustomObject]@{
id = $p.ExternalId;
userName = $p.ExternalId;
givenName = $p.Name.Nickname;
familyName = $surname;
prefix = $AxPrefix;
cost_center = $P.PrimaryContract.CostCenter.Name
job_title = $p.PrimaryContract.Title.Name;
emailAddress = $email;
}
#region helper functions
function Resolve-HTTPError {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
$HttpErrorObj = @{
FullyQualifiedErrorId = $ErrorObject.FullyQualifiedErrorId
InvocationInfo = $ErrorObject.InvocationInfo.MyCommand
TargetObject = $ErrorObject.TargetObject.RequestUri
StackTrace = $ErrorObject.ScriptStackTrace
}
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
}
}
#endregion
if (-not($dryRun -eq $true)) {
try {
Write-Verbose "Updating Axxerion user '$($p.DisplayName)'"
$clobMBValue = @{
action = 'Account Update'
body = @{
id = $account.id;
username = $account.userName;
email = $account.emailAddress;
first_name = $account.givenName;
last_name = $account.familyName;
prefix = $account.prefix;
cost_center = $account.cost_center;
job_title = $account.job_title;
}
} | ConvertTo-Json
$body = @{
datasource = 'HelloID'
clobMBValue = $clobMBValue
} | ConvertTo-Json
$authorization = "$($c.UserName):$($c.Password)"
$base64Credentials = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($authorization))
$headers = @{
"Authorization" = "Basic $base64Credentials"
}
$splatParams = @{
Uri = "$($c.BaseUrl)/webservices/$($c.Customer)/rest/functions/createupdate/ImportItem"
Body = ([System.Text.Encoding]::UTF8.GetBytes($body))
Headers = $headers
Method = 'POST'
}
$updateResponse = Invoke-RestMethod @splatParams
if ($updateResponse.id){
$success = $true
$auditLogs.Add([PSCustomObject]@{
Action = "UpdateAccount"
Message = "Account for '$($p.DisplayName)' successfully updated"
IsError = $false
})
}
} catch {
$ex = $PSItem
$success = $false
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObject = Resolve-HTTPError -Error $ex
$auditMessage = "Could not update user account for '$($p.DisplayName)', Error $($errorObject.ErrorMessage)"
} else {
$auditMessage = "Could not update user account for '$($p.DisplayName)', Error: $($ex.Exception.Message)"
}
$auditLogs.Add([PSCustomObject]@{
Action = "UpdateAccount"
Message = $auditMessage
IsError = $true
})
Write-Verbose $auditLogs.Message
}
}
$result = [PSCustomObject]@{
Success = $success
Account = $account
AuditLogs = $auditLogs
ExportData = [PSCustomObject]@{
displayName = $account.id;
userName = $account.id;
email = $account.emailAddress;
};
}
Write-Output $result | ConvertTo-Json -Depth 10