-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable.ps1
303 lines (255 loc) · 11.5 KB
/
enable.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#################################################
# HelloID-Conn-Prov-Target-Microsoft-Exchange-Online-Enable
# Sets Hide from address list to false
# PowerShell V2
#################################################
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12
# Define PowerShell commands to import
$commands = @(
"Get-EXOMailbox",
"Set-Mailbox"
)
#region functions
function Resolve-ExchangeOnlineError {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[object]
$ErrorObject
)
process {
$httpErrorObj = [PSCustomObject]@{
ScriptLineNumber = $ErrorObject.InvocationInfo.ScriptLineNumber
Line = $ErrorObject.InvocationInfo.Line
ErrorDetails = $ErrorObject.Exception.Message
FriendlyMessage = $ErrorObject.Exception.Message
}
if (-not [string]::IsNullOrEmpty($ErrorObject.ErrorDetails.Message)) {
$httpErrorObj.ErrorDetails = $ErrorObject.ErrorDetails.Message
}
elseif ($ErrorObject.Exception.GetType().FullName -eq 'System.Net.WebException') {
if ($null -ne $ErrorObject.Exception.Response) {
$streamReaderResponse = [System.IO.StreamReader]::new($ErrorObject.Exception.Response.GetResponseStream()).ReadToEnd()
if (-not [string]::IsNullOrEmpty($streamReaderResponse)) {
$httpErrorObj.ErrorDetails = $streamReaderResponse
}
}
}
try {
$errorObjectConverted = $ErrorObject | ConvertFrom-Json -ErrorAction Stop
if ($null -ne $errorObjectConverted.error_description) {
$httpErrorObj.FriendlyMessage = $errorObjectConverted.error_description
}
elseif ($null -ne $errorObjectConverted.error) {
if ($null -ne $errorObjectConverted.error.message) {
$httpErrorObj.FriendlyMessage = $errorObjectConverted.error.message
if ($null -ne $errorObjectConverted.error.code) {
$httpErrorObj.FriendlyMessage = $httpErrorObj.FriendlyMessage + " Error code: $($errorObjectConverted.error.code)"
}
}
else {
$httpErrorObj.FriendlyMessage = $errorObjectConverted.error
}
}
else {
$httpErrorObj.FriendlyMessage = $ErrorObject
}
}
catch {
$httpErrorObj.FriendlyMessage = $httpErrorObj.ErrorDetails
}
Write-Output $httpErrorObj
}
}
function Convert-StringToBoolean($obj) {
if ($obj -is [PSCustomObject]) {
foreach ($property in $obj.PSObject.Properties) {
$value = $property.Value
if ($value -is [string]) {
$lowercaseValue = $value.ToLower()
if ($lowercaseValue -eq "true") {
$obj.$($property.Name) = $true
}
elseif ($lowercaseValue -eq "false") {
$obj.$($property.Name) = $false
}
}
elseif ($value -is [PSCustomObject] -or $value -is [System.Collections.IDictionary]) {
$obj.$($property.Name) = Convert-StringToBoolean $value
}
elseif ($value -is [System.Collections.IList]) {
for ($i = 0; $i -lt $value.Count; $i++) {
$value[$i] = Convert-StringToBoolean $value[$i]
}
$obj.$($property.Name) = $value
}
}
}
return $obj
}
#endregion functions
try {
#region Verify account reference
$actionMessage = "verifying account reference"
if ([string]::IsNullOrEmpty($($actionContext.References.Account))) {
throw "The account reference could not be found"
}
#endregion Verify account reference
#region Import module
$actionMessage = "importing module [ExchangeOnlineManagement]"
$importModuleSplatParams = @{
Name = "ExchangeOnlineManagement"
Cmdlet = $commands
Verbose = $false
ErrorAction = "Stop"
}
$null = Import-Module @importModuleSplatParams
Write-Information "Imported module [$($importModuleSplatParams.Name)]"
#endregion Create access token
#region Create access token
$actionMessage = "creating access token"
$createAccessTokenBody = @{
grant_type = "client_credentials"
client_id = $actionContext.Configuration.AppId
client_secret = $actionContext.Configuration.AppSecret
resource = "https://outlook.office365.com"
}
$createAccessTokenSplatParams = @{
Uri = "https://login.microsoftonline.com/$($actionContext.Configuration.TenantID)/oauth2/token"
Headers = $headers
Method = "POST"
ContentType = "application/x-www-form-urlencoded"
UseBasicParsing = $true
Body = $createAccessTokenBody
Verbose = $false
ErrorAction = "Stop"
}
$createAccessTokenResonse = Invoke-RestMethod @createAccessTokenSplatParams
Write-Information "Created access token."
#endregion Create access token
#region Connect to Microsoft Exchange Online
# Docs: https://learn.microsoft.com/en-us/powershell/module/exchange/connect-exchangeonline?view=exchange-ps
$actionMessage = "connecting to Microsoft Exchange Online"
$createExchangeSessionSplatParams = @{
Organization = $actionContext.Configuration.Organization
AppID = $actionContext.Configuration.AppId
AccessToken = $createAccessTokenResonse.access_token
CommandName = $commands
ShowBanner = $false
ShowProgress = $false
TrackPerformance = $false
SkipLoadingCmdletHelp = $true
SkipLoadingFormatData = $true
ErrorAction = "Stop"
}
$null = Connect-ExchangeOnline @createExchangeSessionSplatParams
Write-Information "Connected to Microsoft Exchange Online"
#endregion Connect to Microsoft Exchange Online
#region Get account
# Docs: https://learn.microsoft.com/en-us/powershell/module/exchange/get-user?view=exchange-ps
$actionMessage = "querying account where [Identity] = [$($actionContext.References.Account)]"
$getMicrosoftExchangeOnlineAccountSplatParams = @{
Identity = $actionContext.References.Account
Properties = 'HiddenFromAddressListsEnabled'
Verbose = $false
ErrorAction = "Stop"
}
$correlatedAccount = Get-EXOMailbox @getMicrosoftExchangeOnlineAccountSplatParams | Select-Object Guid, DisplayName, HiddenFromAddressListsEnabled
Write-Information "Queried account where [Identity] = [$($actionContext.References.Account)]. Result: $($correlatedAccount | ConvertTo-Json)"
#endregion Get account
#region Calulate action
$actionMessage = "calculating action"
if (($correlatedAccount | Measure-Object).count -eq 1) {
if ($correlatedAccount.HiddenFromAddressListsEnabled -eq $true) {
$actionAccount = "Enable"
}
else {
$actionAccount = "NoChanges"
}
}
else {
$actionAccount = "NotFound"
}
#endregion Calulate action
#region Process
switch ($actionAccount) {
"Enable" {
$actionMessage = "enabeling account"
$setMicrosoftExchangeOnlineAccountSplatParams = @{
Identity = $actionContext.References.Account
HiddenFromAddressListsEnabled = $false
Verbose = $false
ErrorAction = "Stop"
}
Write-Information "SplatParams: $($setMicrosoftExchangeOnlineAccountSplatParams | ConvertTo-Json)"
if (-Not($actionContext.DryRun -eq $true)) {
$null = Set-Mailbox @setMicrosoftExchangeOnlineAccountSplatParams
Write-Information "Account with id [$($actionContext.References.Account)] successfully enabled [HideFromAddressListsEnabled = false]"
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Account with id [$($actionContext.References.Account)] successfully enabled [HideFromAddressListsEnabled = false]"
IsError = $false
})
}
else {
Write-Warning "DryRun: Would set account with id [$($actionContext.References.Account)] to [HideFromAddressListsEnabled = false]."
}
break
}
"NoChanges" {
$actionMessage = "no changes to account"
$outputContext.Data = $actionContext.Data
$outputContext.PreviousData = $actionContext.Data
Write-Information "Account with id [$($actionContext.References.Account)] successfully checked. No changes required"
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "Account with id [$($actionContext.References.Account)] successfully checked. No changes required"
IsError = $false
})
break
}
"NotFound" {
$actionMessage = "updating account"
Write-Information "No account found where [Identity] = [$($actionContext.References.Account)]. Possibly indicating that it could be deleted, or not correlated."
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = "No account found where [Identity] = [$($actionContext.References.Account)]. Possibly indicating that it could be deleted, or not correlated."
IsError = $false
})
break
}
}
#endregion Process
}
catch {
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorObj = Resolve-ExchangeOnlineError -ErrorObject $ex
$auditMessage = "Error $($actionMessage). Error: $($errorObj.FriendlyMessage)"
$warningMessage = "Error at Line [$($errorObj.ScriptLineNumber)]: $($errorObj.Line). Error: $($errorObj.ErrorDetails)"
}
else {
$auditMessage = "Error $($actionMessage). Error: $($ex.Exception.Message)"
$warningMessage = "Error at Line [$($ex.InvocationInfo.ScriptLineNumber)]: $($ex.InvocationInfo.Line). Error: $($ex.Exception.Message)"
}
Write-Warning $warningMessage
$outputContext.AuditLogs.Add([PSCustomObject]@{
Message = $auditMessage
IsError = $true
})
}
finally {
#region Disconnect from Microsoft Exchange Online
# Docs: https://learn.microsoft.com/en-us/powershell/module/exchange/disconnect-exchangeonline?view=exchange-ps
$actionMessage = "disconnecting to Microsoft Exchange Online"
$deleteExchangeSessionSplatParams = @{
Confirm = $false
ErrorAction = "Stop"
}
$null = Disconnect-ExchangeOnline @deleteExchangeSessionSplatParams
Write-Information "Disconnected from Microsoft Exchange Online"
#endregion Disconnect from Microsoft Exchange Online
# Check if auditLogs contains errors, if no errors are found, set success to true
if (-NOT($outputContext.AuditLogs.IsError -contains $true)) {
$outputContext.Success = $true
}
}