-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.ps1
163 lines (148 loc) · 5.53 KB
/
delete.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
#####################################################
# HelloID-Conn-Prov-Target-Salesforce-Delete
#
# Version: 1.0.0.2
#####################################################
$VerbosePreference = 'Continue'
# Initialize default value's
$config = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json
$aRef = $AccountReference | ConvertFrom-Json
$success = $false
$auditLogs = New-Object Collections.Generic.List[PSCustomObject]
#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 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
if (-not($dryRun -eq $true)) {
try {
Write-Verbose "Disabling account '$($aRef)' for '$($p.DisplayName)'"
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)
[System.Collections.Generic.List[object]]$operations = @()
$operations.Add(
[PSCustomObject]@{
op = "Replace"
value = @{
active = $false
}
}
)
$body = [ordered]@{
schemas = @(
"urn:ietf:params:scim:api:messages:2.0:PatchOp"
)
Operations = $operations
} | ConvertTo-Json -Depth 10
Write-Verbose 'Adding Authorization headers'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $($accessToken.access_token)")
$splatParams = @{
Uri = "$instanceUri/services/scim/v2/Users/$aRef"
Headers = $headers
Body = $body
Method = 'Patch'
}
$results = Invoke-RestMethod @splatParams
if ($results.id){
$success = $true
$auditLogs.Add([PSCustomObject]@{
Message = "Disable account for: $($p.DisplayName) was successful."
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 disable salesforce account for: $($p.DisplayName). Error: $($errorObj.ErrorMessage)"
} else {
$errorMessage = "Could not disable salesforce account for: $($p.DisplayName). Error: $($ex.Exception.Message)"
}
Write-Error $errorMessage
$auditLogs.Add([PSCustomObject]@{
Message = $errorMessage
IsError = $true
})
} finally {
$result = [PSCustomObject]@{
Success = $success
AuditDetails = $auditMessage
}
Write-Output $result | ConvertTo-Json -Depth 10
}
}