Skip to content

Commit

Permalink
Refactor Invoke-AddRoomMailbox function to improve tenant ID handling…
Browse files Browse the repository at this point in the history
… and error logging
  • Loading branch information
kris6673 committed Nov 5, 2024
1 parent 6883c8b commit 215c05d
Showing 1 changed file with 11 additions and 9 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
})
}

0 comments on commit 215c05d

Please sign in to comment.