-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ps1
310 lines (277 loc) · 11.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
#####################################################
# HelloID-Conn-Prov-Target-Salesforce-Create
#
# Version: 1.0.0.6
#####################################################
$VerbosePreference = "Continue"
# Initialize default value's
$config = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json
$success = $false
$auditLogs = New-Object Collections.Generic.List[PSCustomObject]
$account = [PSCustomObject]@{
ExternalId = $p.ExternalId
UserName = $p.UserName
GivenName = $p.Name.nickName
FamilyName = $p.Name.FamilyName
FamilyNameFormatted = $p.Name.nickName + " " + $p.Name.FamilyName
FamilyNamePrefix = ''
department = $p.PrimaryContract.Department.DisplayName
mobilePhone = $p.Contact.Business.Phone.Mobile
mobilePhoneType = 'Work'
IsUserActive = $true
EmailAddress = $p.Contact.Business.Email
EmailAddressType = 'Work'
emailEncodingKey = 'ISO-8859-1'
IsEmailPrimary = $true
Entitlement = '<Entitlement Profile ID needed here>'
UserType = 'Standard'
}
#region functions
function Get-ScimOAuthToken {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$ClientID,
[Parameter(Mandatory)]
[string]
$ClientSecret,
[Parameter(Mandatory)]
[string]
$UserName,
[Parameter(Mandatory)]
[string]
$Password,
[Parameter(Mandatory)]
[string]
$AuthenticationUri
)
try {
Write-Verbose "Invoking command '$($MyInvocation.MyCommand)'"
$headers = @{
"content-type" = "application/x-www-form-urlencoded"
}
$splatParams = @{
Uri = "$($AuthenticationUri)?grant_type=password&client_id=$($ClientID)&client_secret=$($ClientSecret)&username=$($UserName)&password=$($Password)"
Method = 'POST'
Headers = $Headers
}
Invoke-RestMethod @splatParams
Write-Verbose 'Finished retrieving accessToken'
} catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
function Invoke-ScimRestMethod {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Method,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$InstanceUri,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Endpoint,
[object]
$Body,
[string]
$ContentType = 'application/json',
[Parameter(Mandatory)]
[System.Collections.IDictionary]
$Headers,
[string]
$TotalResults
)
try {
Write-Verbose "Invoking command '$($MyInvocation.MyCommand)'"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
$baseUrl = "$InstanceUri/services/scim/v2"
$splatParams = @{
Headers = $Headers
Method = $Method
ContentType = $ContentType
}
if ($Body){
Write-Verbose 'Adding body to request'
$splatParams['Body'] = $Body
}
if ($TotalResults){
# Fixed value since each page contains 20 items max
$count = 20
$startIndex = 1
[System.Collections.Generic.List[object]]$dataList = @()
Write-Verbose 'Using pagination to retrieve results'
do {
$splatParams['Uri'] = "$($baseUrl)/$($Endpoint)?startIndex=$startIndex&count=$count"
$result = Invoke-RestMethod @splatParams
$null = $startIndex + $count + $count
foreach ($resource in $result.Resources){
$dataList.Add($resource)
}
$startIndex = $count+$startIndex
} until ($dataList.Count -eq $TotalResults)
Write-Output $dataList
} else {
$splatParams['Uri'] = "$baseUrl/$Endpoint"
Invoke-RestMethod @splatParams
}
} catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
function Resolve-HTTPError {
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline
)]
[object]$ErrorObject
)
process {
$HttpErrorObj = [PSCustomObject]@{
FullyQualifiedErrorId = $ErrorObject.FullyQualifiedErrorId
MyCommand = $ErrorObject.InvocationInfo.MyCommand
RequestUri = $ErrorObject.TargetObject.RequestUri
ScriptStackTrace = $ErrorObject.ScriptStackTrace
ErrorMessage = ''
}
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
try {
#Begin
Write-Verbose 'Retrieving accessToken'
$splatTokenParams = @{
ClientID = $($config.ClientID)
ClientSecret = $($config.ClientSecret)
UserName = $($config.UserName)
Password = $($config.Password)
AuthenticationUri = $($config.AuthenticationUri)
}
$accessToken = Get-ScimOAuthToken @splatTokenParams
Write-Verbose 'Adding token to authorization headers'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $($accessToken.access_token)")
Write-Verbose 'Getting instance url'
$instanceUri = $($config.baseUrl)
Write-Verbose "Checking if user [$($account.UserName)] exists"
$response = Invoke-ScimRestMethod -InstanceUri $instanceUri -Endpoint "Users?filter=userName eq ""$($account.username)""" -Method 'GET' -headers $headers
$userObject = $response.Resources
if ($null -ne $userObject.id){
Write-Verbose "Account for '$($p.DisplayName)' found with id '$($userObject.id)', switching to 'correlate'"
$action = 'Correlate'
} else {
Write-Verbose "No account for '$($p.DisplayName)' has been found, switching to 'create'"
$action = 'Create'
}
# Add an auditMessage showing what will happen during enforcement
if ($dryRun){
$auditMessage = "$action Salesforce account for: $($p.DisplayName) will be executed during enforcement"
}
# Process
if (-not ($dryRun -eq $true)){
switch ($action) {
'Create' {
Write-Verbose "Creating account for '$($p.DisplayName)'"
[System.Collections.Generic.List[object]]$emailList = @()
$emailList.Add(
[PSCustomObject]@{
primary = $account.IsEmailPrimary
type = $account.EmailAddressType
display = $account.EmailAddress
value = $account.EmailAddress
}
)
[System.Collections.Generic.List[object]]$entitlementList = @()
$entitlementList.Add(
[PSCustomObject]@{
value = $account.Entitlement
}
)
[System.Collections.Generic.List[object]]$phoneList = @()
$phoneList.Add(
[PSCustomObject]@{
type = $account.mobilePhoneType
value = $account.mobilePhone
}
)
$body = [ordered]@{
schemas = @(
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
)
EmployeeNumber = $account.ExternalID
userName = $account.UserName
emails = $emailList
emailEncodingKey = $account.emailEncodingKey
department = $account.department
entitlements = $entitlementList
userType = $account.UserType
phoneNumbers = $phoneList
meta = @{
resourceType = "User"
}
name = [ordered]@{
formatted = $account.FamilyNameFormatted
familyName = $account.FamilyName
familyNamePrefix = $account.FamilyNamePrefix
givenName = $account.GivenName
}
} | ConvertTo-Json
$response = Invoke-ScimRestMethod -InstanceUri $instanceUri -Endpoint 'Users' -Method 'POST' -body $body -headers $headers
$accountReference = $response.id
break
}
'Correlate'{
Write-Verbose "Correlating account for '$($p.DisplayName)'"
$accountReference = $userObject.id
break
}
}
$success = $true
$auditLogs.Add([PSCustomObject]@{
Message = "$action Salesforce account for: $($p.DisplayName) was successful. AccountReference is: $accountReference"
IsError = $False
})
}
} catch {
$success = $false
$ex = $PSItem
if ( $($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or $($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-HTTPError -Error $ex
$errorMessage = "Could not create Salesforce account for: $($p.DisplayName). Error: $($errorObj.ErrorMessage)"
} else {
$errorMessage = "Could not create Salesforce account for: $($p.DisplayName). Error: $($ex.Exception.Message)"
}
Write-Error $errorMessage
$auditLogs.Add([PSCustomObject]@{
Message = $errorMessage
IsError = $true
})
# End
} Finally {
$result = [PSCustomObject]@{
Success = $success
AccountReference = $accountReference
Auditlogs = $auditLogs
AuditDetails = $auditMessage
Account = $account
}
Write-Output $result | ConvertTo-Json -Depth 10
}