-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.ps1
201 lines (169 loc) · 6.96 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
###################################################################
# HelloID-Conn-Prov-Target-KPNLisa-Update
# PowerShell V2
###################################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
#region functions
function Get-LisaAccessToken {
[CmdletBinding()]
param(
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$TenantId,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$ClientId,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$ClientSecret,
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$Scope,
[Parameter()]
[switch]
$AsSecureString
)
try {
$SplatParams = @{
Uri = "https://login.microsoftonline.com/$($TenantId)/oauth2/v2.0/token/"
ContentType = "application/x-www-form-urlencoded"
Method = "Post"
Body = @{
grant_type = "client_credentials"
client_id = $ClientId
client_secret = $ClientSecret
scope = $Scope
}
}
$Response = Invoke-RestMethod @SplatParams
if ($AsSecureString) {
Write-Output ($Response.access_token | ConvertTo-SecureString -AsPlainText)
}
else {
Write-Output ($Response.access_token)
}
}
catch {
$PSCmdlet.ThrowTerminatingError($PSItem)
}
}
function Resolve-ErrorMessage {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[object]
$ErrorObject
)
process {
$Exception = [PSCustomObject]@{
FullyQualifiedErrorId = $ErrorObject.FullyQualifiedErrorId
MyCommand = $ErrorObject.InvocationInfo.MyCommand
RequestUri = $ErrorObject.TargetObject.RequestUri
ScriptStackTrace = $ErrorObject.ScriptStackTrace
ErrorMessage = $Null
VerboseErrorMessage = $Null
}
switch ($ErrorObject.Exception.GetType().FullName) {
"Microsoft.PowerShell.Commands.HttpResponseException" {
$Exception.ErrorMessage = $ErrorObject.ErrorDetails.Message
break
}
"System.Net.WebException" {
$Exception.ErrorMessage = [System.IO.StreamReader]::new(
$ErrorObject.Exception.Response.GetResponseStream()).ReadToEnd()
break
}
default {
$Exception.ErrorMessage = $ErrorObject.Exception.Message
}
}
$Exception.VerboseErrorMessage = @(
"Error at Line [$($ErrorObject.InvocationInfo.ScriptLineNumber)]: $($ErrorObject.InvocationInfo.Line)."
"ErrorMessage: $($Exception.ErrorMessage) [$($ErrorObject.ErrorDetails.Message)]"
) -Join " "
Write-Output $Exception
}
}
#endregion functions
# Hack for non updatable fields we like to return
# @link: https://helloid.canny.io/provisioning/p/exportdata-in-powershell-v2
$NonUpdatables = @(
"userPrincipalName"
"mail"
)
# Start Script
try {
# Formatting Headers and authentication for KPN Lisa Requests
$LisaRequest = @{
Authentication = "Bearer"
Token = $actionContext.Configuration.AzureAD | Get-LisaAccessToken -AsSecureString
ContentType = "application/json; charset=utf-8"
Headers = @{
"Mwp-Api-Version" = "1.0"
}
}
#Get previous account, select only $outputContext.Data.Keys
$SplatParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/Users/$($actionContext.References.Account)"
Method = "Get"
}
$PreviousPerson = Invoke-RestMethod @LisaRequest @SplatParams
$outputContext.PreviousData = $PreviousPerson | Select-Object -Property ([array] $outputContext.Data.PSObject.Properties.Name)
Write-Verbose -Verbose "Updating KPN Lisa account for '$($personContext.Person.DisplayName)'"
$SplatParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/Users/$($actionContext.References.Account)/bulk"
Method = "Patch"
Body = $outputContext.Data | Select-Object -Property * -ExcludeProperty $NonUpdatables
}
if (-Not ($actionContext.DryRun -eq $True)) {
[void] (Invoke-RestMethod @LisaRequest @SplatParams)
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "UpdateAccount" # Optionally specify a different action for this audit log
Message = "Account for '$($personContext.Person.DisplayName)' Updated. ObjectId: '$($actionContext.References.Account)'"
IsError = $False
})
# Updating manager
if ($Null -eq $actionContext.References.ManagerAccount) {
$SplatParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/Users/$($actionContext.References.Account)/manager"
Method = "Delete"
}
# TODO:: validate return value on update and delete for manager
if (-Not ($actionContext.DryRun -eq $True)) {
[void] (Invoke-RestMethod @LisaRequest @SplatParams)
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "UpdateAccount" # Optionally specify a different action for this audit log
Message = "Manager for '$($personContext.Person.DisplayName)' deleted. ObjectId: '$($UserResponse.objectId)'"
IsError = $False
})
}
else {
$SplatParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/Users/$($actionContext.References.Account)/Manager"
Method = "Put"
Body = $actionContext.References.ManagerAccount
}
# TODO:: validate return value on update and delete for manager
if (-Not ($actionContext.DryRun -eq $True)) {
[void] (Invoke-RestMethod @LisaRequest @SplatParams)
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "UpdateAccount" # Optionally specify a different action for this audit log
Message = "Manager for '$($personContext.Person.DisplayName)' Updated. ObjectId: '$($UserResponse.objectId)'"
IsError = $False
})
}
$outputContext.Success = $True
}
catch {
$Exception = $PSItem | Resolve-ErrorMessage
Write-Verbose -Verbose $Exception.VerboseErrorMessage
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "UpdateAccount" # Optionally specify a different action for this audit log
Message = "Error updating account [$($personContext.Person.DisplayName) ($($actionContext.References.Account))]. Error Message: $($Exception.ErrorMessage)."
IsError = $True
})
}