Skip to content

Commit

Permalink
Merge pull request #1171 from kris6673/dev
Browse files Browse the repository at this point in the history
Refactor shared and room mailbox creation and alias handling logic + error logging
  • Loading branch information
JohnDuprey authored Nov 11, 2024
2 parents 9f562de + f3ff2ba commit 45f4a7f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Function Invoke-AddRoomMailbox {

$APIName = $TriggerMetadata.FunctionName
$User = $request.headers.'x-ms-client-principal'

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

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

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

# Block sign-in for the mailbox
try {
$Request = Set-CIPPSignInState -userid $AddRoomRequest.ExternalDirectoryObjectId -TenantFilter $($MailboxObject.tenantid) -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
$Request = Set-CIPPSignInState -userid $AddRoomRequest.ExternalDirectoryObjectId -TenantFilter $Tenant -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
$Results.add("Blocked sign-in for Room mailbox; $($MailboxObject.userPrincipalName)")
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
$Results.add("Failed to block sign-in for Room mailbox: $($MailboxObject.userPrincipalName). Error: $ErrorMessage")
$ErrorMessage = Get-CippException -Exception $_
$Results.add("Failed to block sign-in for Room mailbox: $($MailboxObject.userPrincipalName). Error: $($ErrorMessage.NormalizedError)")
}

$StatusCode = [HttpStatusCode]::OK
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Failed to create room: $($MailboxObject.DisplayName). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to create room: $($MailboxObject.DisplayName). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
$Results.Add("Failed to create Room mailbox $($MailboxObject.userPrincipalName). $($ErrorMessage.NormalizedError)")
$StatusCode = [HttpStatusCode]::Forbidden
}


$Body = [pscustomobject] @{ 'Results' = @($Results) }
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
StatusCode = $StatusCode
Body = $Body
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ Function Invoke-AddSharedMailbox {

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

# Write to the Azure Functions log stream.
Write-Host 'PowerShell HTTP trigger function processed a request.'

$Results = [System.Collections.ArrayList]@()
$MailboxObject = $Request.body
$Tenant = $MailboxObject.tenantid
$Aliases = $MailboxObject.addedAliases -Split '\n'

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

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

# Block sign-in for the mailbox
try {
$null = Set-CIPPSignInState -userid $AddSharedRequest.ExternalDirectoryObjectId -TenantFilter $($MailboxObject.tenantid) -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
$null = Set-CIPPSignInState -userid $AddSharedRequest.ExternalDirectoryObjectId -TenantFilter $Tenant -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
$Body = $Results.add("Blocked sign-in for shared mailbox $Email")
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
$Body = $Results.add("Failed to block sign-in for shared mailbox $Email. Error: $ErrorMessage")
$ErrorMessage = Get-CippException -Exception $_
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
$Body = $Results.add("Failed to block sign-in for shared mailbox $Email. Error: $($ErrorMessage.NormalizedError)")
}

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

# Add aliases to the mailbox if any are provided
if ($Aliases) {
try {
Start-Sleep 3 # Sleep since there is apparently a race condition with the mailbox creation if we don't delay for a lil bit
$AliasBodyToShip = [pscustomobject] @{
'Identity' = $AddSharedRequest.Guid
'EmailAddresses' = @{'@odata.type' = '#Exchange.GenericHashTable'; Add = $Aliases }
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to add aliases to $Email : $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
$Body = $results.add("ERROR: Failed to add aliases to $Email : $($ErrorMessage.NormalizedError)")
}
$null = New-ExoRequest -tenantid $MailboxObject.tenantid -cmdlet 'Set-Mailbox' -cmdparams $AliasBodyToShip -UseSystemMailbox $true
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Added aliases to $Email : $($Aliases -join ',')" -Sev 'Info'
$Body = $results.add("Added Aliases to $Email : $($Aliases -join ',')")

} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Failed to add aliases to $Email : $ErrorMessage" -Sev 'Error'
$Body = $results.add("ERROR: Failed to add aliases to $Email : $ErrorMessage")
}
$StatusCode = [HttpStatusCode]::OK
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to create shared mailbox. Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
$Body = $Results.add("Failed to create Shared Mailbox. $($ErrorMessage.NormalizedError)")
$StatusCode = [HttpStatusCode]::Forbidden
}


$Body = [pscustomobject] @{ 'Results' = @($results) }
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
StatusCode = $StatusCode
Body = $Body
})

Expand Down
2 changes: 1 addition & 1 deletion Modules/CIPPCore/Public/GraphHelper/New-ExoRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function New-ExoRequest {
Method = 'POST'
Body = $ExoBody
Headers = $Headers
ContentType = 'application/json'
ContentType = 'application/json; charset=utf-8'
}

$Return = Invoke-RestMethod @ExoRequestParams -ResponseHeadersVariable ResponseHeaders
Expand Down

0 comments on commit 45f4a7f

Please sign in to comment.