-
Notifications
You must be signed in to change notification settings - Fork 2
/
disable.ps1
150 lines (127 loc) · 4.72 KB
/
disable.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
###################################################################
# HelloID-Conn-Prov-Target-KPNLisa-Disable
# 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
# 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"
}
}
Write-Verbose -Verbose "Disable KPN Lisa account for '$($personContext.Person.DisplayName)'"
$SplatParams = @{
Uri = "$($actionContext.Configuration.BaseUrl)/Users/$($actionContext.References.Account)"
Method = "Patch"
Body = @{
propertyName = "accountEnabled"
value = $False
}
}
if (-Not ($actionContext.DryRun -eq $True)) {
[void] (Invoke-RestMethod @LisaRequest @SplatParams)
}
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "DisableAccount"
Message = "Account for '$($personContext.Person.DisplayName)' is disabled"
IsError = $False
})
$outputContext.Success = $True
}
catch {
$Exception = $PSItem | Resolve-ErrorMessage
Write-Verbose -Verbose $Exception.VerboseErrorMessage
$outputContext.AuditLogs.Add([PSCustomObject]@{
Action = "DisableAccount" # Optionally specify a different action for this audit log
Message = "Error disabling account [$($personContext.Person.DisplayName) ($($actionContext.References.Account))]. Error Message: $($Exception.ErrorMessage)."
IsError = $True
})
}