Skip to content

Commit

Permalink
Merge pull request #1244 from kris6673/DelegateSentItems
Browse files Browse the repository at this point in the history
Fix DelegateSentItems functionality and enhance logging
  • Loading branch information
KelvinTegelaar authored Jan 19, 2025
2 parents 9f20219 + 618a0ba commit b9cfa1e
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ function Invoke-CIPPStandardDelegateSentItems {
#$Rerun -Type Standard -Tenant $Tenant -API 'DelegateSentItems' -Settings $Settings


# Backwards compatibility for Pre 7.0.5
if ([string]::IsNullOrWhiteSpace($Settings.IncludeUserMailboxes)) {
$Settings.IncludeUserMailboxes = $true
}

if ($Settings.IncludeUserMailboxes -eq $true) {
$Mailboxes = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-Mailbox' -cmdParams @{ RecipientTypeDetails = @('UserMailbox', 'SharedMailbox') } -Select 'Identity,UserPrincipalName,MessageCopyForSendOnBehalfEnabled,MessageCopyForSentAsEnabled' |
Where-Object { $_.MessageCopyForSendOnBehalfEnabled -eq $false -or $_.MessageCopyForSentAsEnabled -eq $false }
} else {
$Mailboxes = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-Mailbox' -cmdParams @{ RecipientTypeDetails = @('SharedMailbox') } -Select 'Identity,UserPrincipalName,MessageCopyForSendOnBehalfEnabled,MessageCopyForSentAsEnabled' |
Where-Object { $_.MessageCopyForSendOnBehalfEnabled -eq $false -or $_.MessageCopyForSentAsEnabled -eq $false }
}


$Mailboxes = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-Mailbox' -cmdParams @{ RecipientTypeDetails = @('UserMailbox', 'SharedMailbox') } |
Where-Object { $_.MessageCopyForSendOnBehalfEnabled -eq $false -or $_.MessageCopyForSentAsEnabled -eq $false }
Write-Host "Mailboxes: $($Mailboxes.count)"
Write-Host "Mailboxes: $($Mailboxes.Count)"
If ($Settings.remediate -eq $true) {
Write-Host 'Time to remediate'

Expand All @@ -47,34 +58,34 @@ function Invoke-CIPPStandardDelegateSentItems {
}
}
}
$BatchResults = New-ExoBulkRequest -tenantid $tenant -cmdletArray @($Request)
$BatchResults = New-ExoBulkRequest -tenantid $Tenant -cmdletArray @($Request)
$BatchResults | ForEach-Object {
if ($_.error) {
$ErrorMessage = Get-NormalizedError -Message $_.error
Write-Host "Failed to apply Delegate Sent Items Style to $($_.target) Error: $ErrorMessage"
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to apply Delegate Sent Items Style to $($_.error.target) Error: $ErrorMessage" -sev Error
$ErrorMessage = Get-CippException -Exception $_.error
Write-Host "Failed to apply Delegate Sent Items Style to $($_.target) Error: $($ErrorMessage.NormalizedError)"
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to apply Delegate Sent Items Style to $($_.error.target) Error: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
}
}
Write-LogMessage -API 'Standards' -tenant $tenant -message "Delegate Sent Items Style applied for $($Mailboxes.count - $BatchResults.Error.Count) mailboxes" -sev Info
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Delegate Sent Items Style applied for $($Mailboxes.Count - $BatchResults.Error.Count) mailboxes" -sev Info
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to apply Delegate Sent Items Style. Error: $ErrorMessage" -sev Error
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to apply Delegate Sent Items Style. Error: $($ErrorMessage.NormalizedError)" -sev Error -LogData $ErrorMessage
}
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Delegate Sent Items Style already enabled.' -sev Info
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Delegate Sent Items Style already enabled.' -sev Info

}
}
if ($Settings.alert -eq $true) {
if ($null -eq $Mailboxes) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Delegate Sent Items Style is enabled for all mailboxes' -sev Info
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Delegate Sent Items Style is enabled for all mailboxes' -sev Info
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Delegate Sent Items Style is not enabled for $($Mailboxes.count) mailboxes" -sev Alert
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Delegate Sent Items Style is not enabled for $($Mailboxes.Count) mailboxes" -sev Alert
}
}

if ($Settings.report -eq $true) {
$Filtered = $Mailboxes | Select-Object -Property UserPrincipalName, MessageCopyForSendOnBehalfEnabled, MessageCopyForSentAsEnabled
Add-CIPPBPAField -FieldName 'DelegateSentItems' -FieldValue $Filtered -StoreAs json -Tenant $tenant
Add-CIPPBPAField -FieldName 'DelegateSentItems' -FieldValue $Filtered -StoreAs json -Tenant $Tenant
}
}

0 comments on commit b9cfa1e

Please sign in to comment.