Skip to content

Commit

Permalink
Refactor Invoke-AddSharedMailbox function to enhance tenant ID handli…
Browse files Browse the repository at this point in the history
…ng and improve error logging
  • Loading branch information
kris6673 committed Nov 5, 2024
1 parent 215c05d commit f3ff2ba
Showing 1 changed file with 18 additions and 15 deletions.
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,17 +32,18 @@ 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)")
}

# Add aliases to the mailbox if any are provided
Expand All @@ -51,21 +54,21 @@ Function Invoke-AddSharedMailbox {
'Identity' = $AddSharedRequest.Guid
'EmailAddresses' = @{'@odata.type' = '#Exchange.GenericHashTable'; Add = $Aliases }
}
$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'
$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 ',')")

} 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")
$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)")
}
}
$StatusCode = [HttpStatusCode]::OK
} 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")
$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
}

Expand Down

0 comments on commit f3ff2ba

Please sign in to comment.