Skip to content

Commit 45f4a7f

Browse files
authored
Merge pull request KelvinTegelaar#1171 from kris6673/dev
Refactor shared and room mailbox creation and alias handling logic + error logging
2 parents 9f562de + f3ff2ba commit 45f4a7f

3 files changed

Lines changed: 45 additions & 38 deletions

File tree

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Invoke-AddRoomMailbox.ps1

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Function Invoke-AddRoomMailbox {
1212

1313
$APIName = $TriggerMetadata.FunctionName
1414
$User = $request.headers.'x-ms-client-principal'
15+
1516
Write-LogMessage -user $User -API $APINAME -message 'Accessed this API' -Sev 'Debug'
1617

1718
# Write to the Azure Functions log stream.
@@ -20,6 +21,7 @@ Function Invoke-AddRoomMailbox {
2021

2122
$Results = [System.Collections.Generic.List[Object]]::new()
2223
$MailboxObject = $Request.body
24+
$Tenant = $MailboxObject.tenantid
2325
$AddRoomParams = [pscustomobject]@{
2426
Name = $MailboxObject.username
2527
DisplayName = $MailboxObject.displayName
@@ -30,30 +32,30 @@ Function Invoke-AddRoomMailbox {
3032
}
3133
# Interact with query parameters or the body of the request.
3234
try {
33-
$AddRoomRequest = New-ExoRequest -tenantid $($MailboxObject.tenantid) -cmdlet 'New-Mailbox' -cmdparams $AddRoomParams
35+
$AddRoomRequest = New-ExoRequest -tenantid $Tenant -cmdlet 'New-Mailbox' -cmdparams $AddRoomParams
3436
$Results.Add("Successfully created room: $($MailboxObject.DisplayName).")
35-
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Created room $($MailboxObject.DisplayName) with id $($AddRoomRequest.id)" -Sev 'Info'
37+
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Created room $($MailboxObject.DisplayName) with id $($AddRoomRequest.id)" -Sev 'Info'
3638

3739
# Block sign-in for the mailbox
3840
try {
39-
$Request = Set-CIPPSignInState -userid $AddRoomRequest.ExternalDirectoryObjectId -TenantFilter $($MailboxObject.tenantid) -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
41+
$Request = Set-CIPPSignInState -userid $AddRoomRequest.ExternalDirectoryObjectId -TenantFilter $Tenant -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
4042
$Results.add("Blocked sign-in for Room mailbox; $($MailboxObject.userPrincipalName)")
4143
} catch {
42-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
43-
$Results.add("Failed to block sign-in for Room mailbox: $($MailboxObject.userPrincipalName). Error: $ErrorMessage")
44+
$ErrorMessage = Get-CippException -Exception $_
45+
$Results.add("Failed to block sign-in for Room mailbox: $($MailboxObject.userPrincipalName). Error: $($ErrorMessage.NormalizedError)")
4446
}
45-
47+
$StatusCode = [HttpStatusCode]::OK
4648
} catch {
4749
$ErrorMessage = Get-CippException -Exception $_
48-
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Failed to create room: $($MailboxObject.DisplayName). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
50+
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to create room: $($MailboxObject.DisplayName). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
4951
$Results.Add("Failed to create Room mailbox $($MailboxObject.userPrincipalName). $($ErrorMessage.NormalizedError)")
52+
$StatusCode = [HttpStatusCode]::Forbidden
5053
}
5154

52-
5355
$Body = [pscustomobject] @{ 'Results' = @($Results) }
5456
# Associate values to output bindings by calling 'Push-OutputBinding'.
5557
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
56-
StatusCode = [HttpStatusCode]::OK
58+
StatusCode = $StatusCode
5759
Body = $Body
5860
})
5961
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Invoke-AddSharedMailbox.ps1

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ Function Invoke-AddSharedMailbox {
1515

1616
Write-LogMessage -user $User -API $APINAME -message 'Accessed this API' -Sev 'Debug'
1717

18+
# Write to the Azure Functions log stream.
19+
Write-Host 'PowerShell HTTP trigger function processed a request.'
20+
1821
$Results = [System.Collections.ArrayList]@()
1922
$MailboxObject = $Request.body
23+
$Tenant = $MailboxObject.tenantid
2024
$Aliases = $MailboxObject.addedAliases -Split '\n'
2125

22-
# Write to the Azure Functions log stream.
23-
Write-Host 'PowerShell HTTP trigger function processed a request.'
2426
try {
2527

2628
$Email = "$($MailboxObject.username)@$($MailboxObject.domain)"
@@ -30,48 +32,51 @@ Function Invoke-AddSharedMailbox {
3032
'primarySMTPAddress' = $Email
3133
Shared = $true
3234
}
33-
$AddSharedRequest = New-ExoRequest -tenantid $MailboxObject.tenantid -cmdlet 'New-Mailbox' -cmdparams $BodyToShip
35+
$AddSharedRequest = New-ExoRequest -tenantid $Tenant -cmdlet 'New-Mailbox' -cmdparams $BodyToShip
3436
$Body = $Results.add("Successfully created shared mailbox: $Email.")
35-
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Created shared mailbox $($MailboxObject.displayname) with email $Email" -Sev 'Info'
37+
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Created shared mailbox $($MailboxObject.displayname) with email $Email" -Sev 'Info'
3638

3739
# Block sign-in for the mailbox
3840
try {
39-
$null = Set-CIPPSignInState -userid $AddSharedRequest.ExternalDirectoryObjectId -TenantFilter $($MailboxObject.tenantid) -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
41+
$null = Set-CIPPSignInState -userid $AddSharedRequest.ExternalDirectoryObjectId -TenantFilter $Tenant -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
4042
$Body = $Results.add("Blocked sign-in for shared mailbox $Email")
4143
} catch {
42-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
43-
$Body = $Results.add("Failed to block sign-in for shared mailbox $Email. Error: $ErrorMessage")
44+
$ErrorMessage = Get-CippException -Exception $_
45+
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to block sign-in for shared mailbox $Email. Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
46+
$Body = $Results.add("Failed to block sign-in for shared mailbox $Email. Error: $($ErrorMessage.NormalizedError)")
4447
}
4548

46-
} catch {
47-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
48-
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Failed to create shared mailbox. Error: $ErrorMessage" -Sev 'Error'
49-
$Body = $Results.add("Failed to create Shared Mailbox. $ErrorMessage")
50-
}
49+
# Add aliases to the mailbox if any are provided
50+
if ($Aliases) {
51+
try {
52+
Start-Sleep 3 # Sleep since there is apparently a race condition with the mailbox creation if we don't delay for a lil bit
53+
$AliasBodyToShip = [pscustomobject] @{
54+
'Identity' = $AddSharedRequest.Guid
55+
'EmailAddresses' = @{'@odata.type' = '#Exchange.GenericHashTable'; Add = $Aliases }
56+
}
57+
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'Set-Mailbox' -cmdparams $AliasBodyToShip -UseSystemMailbox $true
58+
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Added aliases to $Email : $($Aliases -join ',')" -Sev 'Info'
59+
$Body = $results.add("Added Aliases to $Email : $($Aliases -join ',')")
5160

52-
# Add aliases to the mailbox if any are provided
53-
if ($Aliases) {
54-
try {
55-
Start-Sleep 3 # Sleep since there is apparently a race condition with the mailbox creation if we don't delay for a lil bit
56-
$AliasBodyToShip = [pscustomobject] @{
57-
'Identity' = $AddSharedRequest.Guid
58-
'EmailAddresses' = @{'@odata.type' = '#Exchange.GenericHashTable'; Add = $Aliases }
61+
} catch {
62+
$ErrorMessage = Get-CippException -Exception $_
63+
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to add aliases to $Email : $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
64+
$Body = $results.add("ERROR: Failed to add aliases to $Email : $($ErrorMessage.NormalizedError)")
5965
}
60-
$null = New-ExoRequest -tenantid $MailboxObject.tenantid -cmdlet 'Set-Mailbox' -cmdparams $AliasBodyToShip -UseSystemMailbox $true
61-
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Added aliases to $Email : $($Aliases -join ',')" -Sev 'Info'
62-
$Body = $results.add("Added Aliases to $Email : $($Aliases -join ',')")
63-
64-
} catch {
65-
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
66-
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Failed to add aliases to $Email : $ErrorMessage" -Sev 'Error'
67-
$Body = $results.add("ERROR: Failed to add aliases to $Email : $ErrorMessage")
6866
}
67+
$StatusCode = [HttpStatusCode]::OK
68+
} catch {
69+
$ErrorMessage = Get-CippException -Exception $_
70+
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to create shared mailbox. Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
71+
$Body = $Results.add("Failed to create Shared Mailbox. $($ErrorMessage.NormalizedError)")
72+
$StatusCode = [HttpStatusCode]::Forbidden
6973
}
7074

75+
7176
$Body = [pscustomobject] @{ 'Results' = @($results) }
7277
# Associate values to output bindings by calling 'Push-OutputBinding'.
7378
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
74-
StatusCode = [HttpStatusCode]::OK
79+
StatusCode = $StatusCode
7580
Body = $Body
7681
})
7782

Modules/CIPPCore/Public/GraphHelper/New-ExoRequest.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function New-ExoRequest {
139139
Method = 'POST'
140140
Body = $ExoBody
141141
Headers = $Headers
142-
ContentType = 'application/json'
142+
ContentType = 'application/json; charset=utf-8'
143143
}
144144

145145
$Return = Invoke-RestMethod @ExoRequestParams -ResponseHeadersVariable ResponseHeaders

0 commit comments

Comments
 (0)