-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ps1
269 lines (245 loc) · 10.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
$p = $person | ConvertFrom-Json
$aRef = $AccountReference | ConvertFrom-Json
$mRef = $managerAccountReference | ConvertFrom-Json
$config = $configuration | ConvertFrom-Json
$success = $true
$auditLogs =[System.Collections.Generic.List[PSCustomObject]]::New()
try {
Import-Module $config.ModuleLocation -Force
Initialize-KPNBartServiceClients -username $config.UserName -password $Config.password -BaseUrl $config.Url
} catch {
throw("Initialize-KPNBartServiceClients failed with error: $($_.Exception.Message)")
}
Write-verbose -verbose -message 2
$account = @{
#GivenName = $p.Name.NickName
#SN = $p.Custom.KpnBartLastName
#Initials = $p.Name.Initials
#DisplayName = $p.Custom.KpnBartDisplayName
managerObjectGuid = $mRef
Department = $p.PrimaryContract.Department.DisplayName
TelephoneNumber = $p.contact.Business.phone.mobile
OtherMobile = $p.contact.Personal.phone.mobile
Title = $p.PrimaryContract.Title.Name
ExtensionAttribute2 = $p.PrimaryContract.CostCenter.ExternalId
}
Write-verbose -verbose -message 3
#specify the identity of the object that must be updated
$queryObjectIdentity = [KPNBartConnectedServices.QueryService.ObjectIdentity]::new()
$queryObjectIdentity.IdentityType = "Guid"
$queryObjectIdentity.Value = $aRef
$bulkObjectIdentity = [KPNBartConnectedServices.BulkCommandService.ObjectIdentity]::new()
$bulkObjectIdentity.IdentityType = $queryObjectIdentity.IdentityType
$bulkObjectIdentity.Value = $queryObjectIdentity.value
$commandObjectIdentity = [KPNBartConnectedServices.CommandService.ObjectIdentity]::new()
$commandObjectIdentity.IdentityType = $queryObjectIdentity.IdentityType
$commandObjectIdentity.Value = $queryObjectIdentity.Value
# Set attributes to retrieve
$accountUpdateForAttributes = @()
ForEach ($key in $account.Keys) { $accountUpdateForAttributes += $key }
try {
[string[]]$ObjectAttributes = @($accountUpdateForAttributes)
$previousAccount = Get-KPNBartuser -Identity $queryObjectIdentity -Attributes $objectAttributes
#Write-Verbose -Verbose ($previousAccount | ConvertTo-Json)
} catch {
$auditMessage = "Get-KPNBartuser failed with error: $($_.Exception.Message)"
Write-Verbose -Verbose -Message $auditMessage
throw($auditMessage)
}
$accountUpdateAttributes = @()
ForEach ($attribute in $account.GetEnumerator()) {
if ([string]$attribute.value -ne [string]$previousAccount.$($($attribute.Key)) -and $attribute.key -ne 'managerObjectGuid') {
Write-Verbose -Verbose "Update required for attribute '$($attribute.key)' (Current: '$($previousAccount.$($($attribute.Key)))', new: '$($attribute.value)')"
# create new updateAttributes object
$accountUpdateAttributes += $attribute.key
}
}
Write-verbose -verbose -message 4
try {
$previousAccountManager = Get-KPNBartUserManager -Identity $queryObjectIdentity
#write-verbose -verbose ($previousAccountManager | ConvertTo-Json)
} catch {
throw("Get-KPNBartUserManager failed with error: $($_.Exception.Message)")
}
Write-verbose -verbose -message 5
$accountUpdate = @{}
ForEach ($key in $account.Keys) {
if ($accountUpdateAttributes.contains($key)) {
$accountUpdate.Add($key,$account[$key])
}
}
$CommandList = [System.Collections.Generic.List[KPNBartConnectedServices.BulkCommandService.ModifyADAttributeCommand]]::New()
foreach ($keyValue in $accountUpdate.GetEnumerator())
{
$modifyCommand = [KPNBartConnectedServices.BulkCommandService.ModifyADAttributeCommand]::new()
$modifyCommand.ObjectIdentity = $bulkObjectIdentity
$modifyCommand.Attribute = $keyValue.Key
$modifyCommand.Value = $keyValue.Value
$CommandList.add($modifyCommand)
Write-Verbose -Verbose "Updating attribute '$($keyValue.Key)' to value '$($keyValue.Value)'"
}
if (-not ($dryRun -eq $true))
{
try {
if ($CommandList.count -ne 0) {
$UpdateResults = Set-KPNBartUserAttributeMultiple -CommandList $CommandList
foreach ($Result in $UpdateResults){
if(-not $Result.error -eq $false) {
$success = $false
$auditMessage = "user-create for person $($p.DisplayName). Update of specific attribute failed with Error: '$($Result.command.attribute)' to value '$($Result.command.value)'. Error: $($Result.Message)"
Write-Verbose -Verbose $auditMessage
$auditLogs.Add([PSCustomObject]@{
action = "UpdateAccount"
Message = $auditMessage
IsError = $true
})
}
}
}
} catch {
$auditMessage = "Set-KPNBartUserAttributeMultiple returned error $($_.Exception.Message)"
Write-Verbose -Verbose -Message $auditMessage
throw($auditMessage)
}
}
# Not used in this implementation
# # Set aliases before setting primary
# might add this part to the account attribute
# $emailAliases =[System.Collections.Generic.List[string]]::New()
# $emailAliases.Add($p.Contact.Business.Email)
# $emailAliases.Add("$($p.Name.FamilyName)$($p.Name.GivenName)@test7.local")
# $emailAliases.Add("$($p.Name.FamilyName)$($p.Name.GivenName)@test8.local")
# $emailAliasesToRemove = [System.Collections.Generic.List[string]]::New()
# $emailAliasesToRemove.Add("$($p.Name.FamilyName)$($p.Name.GivenName)@test5.local")
# if ($accountSpecialAttributes.UpdateExchangeWhenSettingMailAttribute -eq $true)
# {
# foreach ($emailAlias in $accountSpecialAttributes.EmailAliasesToAdd)
# {
# if (-not ($dryRun -eq $true)) {
# try{
# New-KPNBartEmailAlias -Identity $commandObjectIdentity -EmailAddress $emailAlias
# }
# catch {
# throw("New-KPNBartEmailAlias returned error $($_.Exception.Message)")
# }
# }
# }
# }
# Not used in this implementation
# # Set mail
# if ( -not [string]::IsNullOrEmpty($accountSpecialAttributes.Mail))
# {
# if ($accountSpecialAttributes.UpdateExchangeWhenSettingMailAttribute -eq $true)
# {
# if (-not ($dryRun -eq $true)) {
# try{
# Set-KPNBartEmailPrimaryAddress -Identity $commandObjectIdentity -EmailAddress $accountSpecialAttributes.Mail
# }
# catch {
# throw("Set-KPNBartEmailPrimaryAddress returned error $($_.Exception.Message)")
# }
# }
# }
# else
# {
# if (-not ($dryRun -eq $true)) {
# try{
# Set-KPNBartEmailADAttribute -Identity $commandObjectIdentity -EmailAddress $accountSpecialAttributes.Mail
# }
# catch {
# throw("Set-KPNBartEmailADAttribute returned error $($_.Exception.Message)")
# }
# }
# }
# }
# Not used in this implementation
# # remove aliases after setting primary
# if ($accountSpecialAttributes.UpdateExchangeWhenSettingMailAttribute -eq $true)
# {
# foreach ($emailAlias in $accountSpecialAttributes.EmailAliasesToRemove)
# {
# if (-not ($dryRun -eq $true)) {
# try{
# Remove-KPNBartEmailAlias -Identity $commandObjectIdentity -EmailAddress $emailAlias
# }
# catch {
# throw("Remove-KPNBartEmailAlias returned error $($_.Exception.Message)")
# }
# }
# }
# }
Write-verbose -verbose -message 7
# Set Manager
if ( -not [string]::IsNullOrEmpty($mRef)) {
Write-verbose -verbose -message "8.1"
write-verbose -verbose ($previousAccountManager| ConvertTo-Json)
if ($previousAccountManager.ObjectGuid -ne $mRef) {
$managerIdentity = [KPNBartConnectedServices.CommandService.ObjectIdentity]::new()
$managerIdentity.IdentityType = "Guid"
$managerIdentity.Value = $mRef
if (-not ($dryRun -eq $true)) {
try {
Set-KPNBartUserManager -Identity $commandObjectIdentity -ManagerIdentity $managerIdentity
$auditMessage = "user-update for person " + $p.DisplayName + ". Manager update succesful."
Write-Verbose -Verbose $auditMessage
$auditLogs.Add([PSCustomObject]@{
action = "UpdateAccount"
Message = $auditMessage
IsError = $false
})
} catch {
$success = $false
$auditMessage = "user-create for person " + $p.DisplayName + ". Update of manager failed with Error: $($_.Exception.Message)".
$auditLogs.Add([PSCustomObject]@{
action = "UpdateAccount"
Message = $auditMessage
IsError = $true
})
$auditMessage = "Set-KPNBartUserManager returned error $($_.Exception.Message)"
Write-Verbose -Verbose -Message $auditMessage
throw($auditMessage)
}
} else {
Write-Verbose -Verbose -Message "dryRun: Setting manager"
}
} else {
Write-Verbose -Verbose -Message "Manager already configured correctly. Update not required."
}
} else {
#$success = $false
$auditMessage = "user-update for person " + $p.DisplayName + ". Update of manager failed with Error: Manager is empty"
Write-Verbose -Verbose -Message $auditMessage
$auditLogs.Add([PSCustomObject]@{
action = "UpdateAccount"
Message = $auditMessage
IsError = $true
})
}
#Not used in this implementation, persona is set once.
# # Set persona
# if (-not [string]::IsNullOrEmpty($accountSpecialAttributes.Persona))
# {
# if (-not ($dryRun -eq $true)) {
# try{
# Set-KPNBartUserPersona -Identity $commandObjectIdentity -Persona $accountSpecialAttributes.Persona
# }
# catch {
# throw("Set-KPNBartUserPersona returned error $($_.Exception.Message)")
# }
# }
# }
if ($success -eq $true) {
$auditMessage = "user-update for person " + $p.DisplayName + " succeeded"
$auditLogs.Add([PSCustomObject]@{
action = "UpdateAccount"
Message = $auditMessage
IsError = $false
})
}
$result = [PSCustomObject]@{
Success = $success
AuditLogs = $auditLogs
Account = $account
}
#send result back
Write-Output $result | ConvertTo-Json -Depth 10