-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ps1
265 lines (233 loc) · 9.44 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
#####################################################
# HelloID-Conn-Prov-Target-SAP-SuccessFactors
#
# Version: 1.0.0
#####################################################
#region Functions
function New-BasicBase64 {
[CmdletBinding()]
param(
[string]
$UserName,
[string]
$PlainPassword
)
$pair = "$($UserName):$($plainPassword)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
return ("Basic $encodedCreds")
}
<# .SYNOPSIS #>
function Set-SAPSFMailAddress {
[CmdletBinding()]
param(
[parameter(Mandatory)] # PersonIdExternal Sap-SuccessFoctors
[string]
$PersonIdExternal,
[parameter(Mandatory)] # Authentication Headers
[System.Collections.Hashtable]
$Headers,
[parameter(Mandatory)] # URL of Sap-SuccessFoctors
[string]
$Url,
[parameter(Mandatory)]
[string]
$MailAddress,
[parameter(Mandatory, ParameterSetName = 'EmailType')] # If the email address is for example always Business, you can enter the ID of the email type in parameter EmailID and leave EmailType empty
# this will skip a lookup at SAP-SuccessFactors
[ValidateSet("Business", "Other", "Personal")]
[string]
$EmailType,
[parameter(Mandatory, ParameterSetName = 'EmailID')]
[int]
$EmailID,
[parameter(Mandatory)] # In SAP-SuccessFactors you must have one Primary Eamil address. So if you mark the new Mailaddress primary the existing addresses are demoted as secondary.
[bool]
$IsPrimary
)
try {
if ($PSCmdlet.ParameterSetName -eq 'EmailType' ) {
$resultEmailLabels = Invoke-SAPSFRestMethod -Url "$url/Picklist('ecEmailType')?`$expand=picklistOptions/picklistLabels" -headers $Headers
$emailLabels = Format-PickListLabel -ResultLabels $resultEmailLabels
$EmailID = $emailLabels[$EmailType]
}
$postBody = @(
[ordered]@{
__metadata = @{
uri = "$Url/PerEmail(emailType='$EmailID',personIdExternal='$PersonIdExternal')"
type = "SFOData.PerEmail"
}
emailType = "$EmailID"
isPrimary = $IsPrimary
emailAddress = $MailAddress
}
)
$getUserEmailRequestURL = $Url + '/PerEmail?$filter=personIdExternal eq ' + "$PersonIdExternal"
$currentSAPSuccessFactors = (Invoke-RestMethod -Headers $Headers -Uri $getUserEmailRequestURL -Method Get).d.results
$currentSAPPrimaryEmailAddress = $currentSAPSuccessFactors | Where-Object { $_.isPrimary -eq "True" }
#Check if there is allready a primary addres in SAP
if ($isPrimary -eq $true -and $emailId -ne $currentSAPPrimaryEmailAddress.emailType) {
$postBodyExistingSAPEmail = @{
__metadata = @{
uri = "$Url/PerEmail(emailType='$($currentSAPPrimaryEmailAddress.emailType)',personIdExternal='$PersonIdExternal')"
type = "SFOData.PerEmail"
}
isPrimary = $false
}
$postBody += $postBodyExistingSAPEmail
}
$jsonBody = ConvertTo-Json -InputObject $postBody
$updateUrl = $Url + '/upsert?$format=json&purgeType=incremental'
return (Invoke-RestMethod -Headers $Headers -Uri $updateUrl -Method Post -ContentType "application/json" -Body $jsonBody).d
} catch {
if ($_.ErrorDetails) {
$errorExceptionDetails = $_.ErrorDetails
}
throw "Could not Set-SAPSFMailAddress , message: $($_.Exception.Message), $errorExceptionDetails".Trim(" ")
}
}
<# .SYNOPSIS #>
function Set-SAPPersonalProperty {
[CmdletBinding()]
param(
[parameter(Mandatory)] # PersonIdExternal Sap-SuccessFoctors
[string]
$PersonIdExternal,
[parameter(Mandatory)] # Authentication Headers
[System.Collections.Hashtable]
$Headers,
[parameter(Mandatory)] # URL of Sap-SuccessFoctors
[string]
$Url,
[parameter(Mandatory)]# Example Hashtable: @{ "CustomString5" = "[email protected]"}
[System.Collections.Hashtable]
$PropertiesKeyValue
)
try {
$postBody = [ordered]@{
__metadata = @{
uri = "$Url/PerPersonal(personIdExternal='$PersonIdExternal',startDate=datetime'2020-01-01T00:00:00')"
type = "SFOData.PerPersonal"
}
}
$postBody += $PropertiesKeyValue
$jsonBody = ConvertTo-Json -InputObject $postBody
$updateUrl = $Url + '/upsert?$format=json&purgeType=incremental'
$resultUpdateProperty = (Invoke-RestMethod -Headers $Headers -Uri $updateUrl -Method Post -ContentType "application/json" -Body $jsonBody).d
return $resultUpdateProperty
} catch {
if ($_.ErrorDetails) {
$errorExceptionDetails = $_.ErrorDetails
}
throw "Could not Set-SAPPersonalProperty , message: $($_.Exception.Message), $errorExceptionDetails".Trim(" ")
}
}
function Invoke-SAPSFRestMethod {
[CmdletBinding()]
param (
[parameter(Mandatory)]
[string]
$Url,
[System.Collections.Hashtable]
$headers,
[int]
$BatchSize = 250
)
try {
$offset = 0
[System.Collections.Generic.List[PSCustomObject]]$returnValue = @()
while ($returnValue.count -eq $offset) {
# Make sure function works with and wihtout query parameters in the Url
if ($Url.Contains("?")) {
$urlWithOffSet = $Url + "&`$skip=$offset&`$top=$BatchSize"
} else {
$urlWithOffSet = $Url + "?`$skip=$offset&`$top=$BatchSize"
}
$rawResponse = Invoke-RestMethod -Uri $urlWithOffSet.ToString() -Method GET -ContentType "application/json" -Headers $headers
if ($rawResponse.d.results.count -gt 0) {
[System.Collections.Generic.List[PSCustomObject]]$partialResult = $rawResponse.d.results
} else {
[System.Collections.Generic.List[PSCustomObject]]$partialResult = $rawResponse.d
}
$returnValue.AddRange($partialResult)
$offset += $BatchSize
}
} catch {
if ($_.ErrorDetails) {
$errorExceptionDetails = $_.ErrorDetails
}
throw "Could not Invoke-SAPSFRestMethod, message: $($_.Exception.Message), $errorExceptionDetails".Trim(" ")
}
return $returnValue
}
function Format-PickListLabel($ResultLabels) {
$labelList = @{ }
foreach ($label in $resultLabels.picklistOptions.results) {
$labelList += @{
($label.picklistLabels.results | where { $_.locale -eq "en_US" }).label = $label.id
}
}
return $labelList
}
#endregion Functions
$verbosePreference = "continue"
#Initialize default properties
$config = ConvertFrom-Json $configuration
if ($config.UseUsernamePassword) {
$Headers = @{
Authorization = (New-BasicBase64 -UserName $config.userName -PlainPassword $config.password )
accept = "application/json"
}
} else {
$Headers = @{
APIKey = $config.APIKey
accept = "application/json"
}
}
$url = $config.url.trim("/")
$p = $person | ConvertFrom-Json;
$m = $manager | ConvertFrom-Json;
$success = $False;
$auditMessage = "identity for person " + $p.DisplayName + " not updated successfully";
#Change mapping here
$account = [PSCustomObject]@{
DisplayName = $p.DisplayName;
FirstName = $p.Name.NickName;
LastName = $p.Name.FamilyName;
ExternalId = $p.ExternalId
UserPrincipalName = $p.Accounts.MicrosoftActiveDirectory.userPrincipalName;
SAPPrimaryEmail = ($p.Accounts.MicrosoftActiveDirectory.proxyAddresses | Where-Object { $_ -cmatch "SMTP" }).replace("SMTP:", "") + ".com" ##
}
if (-Not($dryRun -eq $true)) {
try {
#Update Email
$SAPSFMailAddress = @{
PersonIdExternal = $account.ExternalId
MailAddress = $account.SAPPrimaryEmail
EMailType = "Business"
# -or EmailID = 8446
IsPrimary = $true
Headers = $Headers
url = $url
}
$result = Set-SAPSFMailAddress @SAPSFMailAddress -ErrorAction Stop
#Update UserPrincipalName
$propertiesKeyValue = @{ # In Sap a UserPrincipalName must be added in a CustomField
"CustomString51" = $account.userPrincipalName
#"businessLastName" = $account.UserPrincipalName
}
$result = Set-SAPPersonalProperty -Url $url -PersonIdExternal $account.ExternalId -Headers $Headers -PropertiesKeyValue $propertiesKeyValue -ErrorAction Stop
$success = $True;
$auditMessage = "Successfully Updated identity for person " + $p.DisplayName;
} catch {
$auditMessage = " . $($_.Exception.Message)"
}
}
#build up result
$result = [PSCustomObject]@{
Success = $success;
AccountReference = $account.externalId
AuditDetails = $auditMessage;
Account = $account;
};
#send result back
Write-Output $result | ConvertTo-Json -Depth 10