# Define path for HTML report $outputFile = "$env:USERPROFILE\Downloads\SharedMailboxMessageCopySettingsReport(Navitas).html" # Define path for server address and credentials $serverAddressFile = ".\DefaultServer.txt" $credentialFile = ".\AdminCredential.xml" # --- HTML Styling (CSS) --- $cssStyle = @" "@ # --- Script Logic --- $session = $null # Initialize session variable # --- Initialize Report Body with Title and Descriptions --- # Start with the main H1 title $reportBody = "
Understanding Message Copy Settings:
MessageCopyForSendOnBehalfEnabled): When True, a copy of emails sent 'on behalf of' this shared mailbox is saved to the shared mailbox's Sent Items folder. When False, the copy is saved only to the sender's Sent Items folder.MessageCopyForSentAsEnabled): When True, a copy of emails sent 'as' this shared mailbox is saved to the shared mailbox's Sent Items folder. When False, the copy is saved only to the sender's Sent Items folder.This script reports on these settings and allows enabling 'SentAs Message Copy' (MessageCopyForSentAsEnabled = $true) in bulk.
No shared mailboxes found.
" } # --- Filter Mailboxes for Update --- $mailboxesToUpdate = $mailboxes | Where-Object { $null -ne $_.MessageCopyForSentAsEnabled -and [bool]($_.MessageCopyForSentAsEnabled) -eq $false } # --- Update Logic --- if ($mailboxesToUpdate.Count -gt 0) { Write-Host "$($mailboxesToUpdate.Count) mailboxes identified with SentAs Message Copy disabled:" -ForegroundColor Yellow $mailboxesToUpdate | ForEach-Object { Write-Host " - $($_.DisplayName) ($($_.PrimarySmtpAddress))" } $response = Read-Host "Do you want to ENABLE SentAs Message Copy for these $($mailboxesToUpdate.Count) shared mailboxes? (Y/N)" if ($response -eq 'Y') { Write-Host "Starting update process..." $updateLog = foreach ($mb in $mailboxesToUpdate) { $updateStatus = "OK" $updateMessage = "Successfully enabled SentAs Message Copy for $($mb.DisplayName) ($($mb.PrimarySmtpAddress))." try { Set-Mailbox -Identity $mb.PrimarySmtpAddress -MessageCopyForSentAsEnabled $true -ErrorAction Stop Write-Host $updateMessage -ForegroundColor Green } catch { $updateStatus = "Error" $updateMessage = "Failed to enable SentAs Message Copy for $($mb.DisplayName) ($($mb.PrimarySmtpAddress)). Error: $($_.Exception.Message)" Write-Host $updateMessage -ForegroundColor Red } [PSCustomObject]@{ Mailbox = "$($mb.DisplayName) ($($mb.PrimarySmtpAddress))" Status = $updateStatus Details = $updateMessage } } $reportBody += "Could not retrieve updated mailbox information.
" } Write-Host "Bulk update process completed." -ForegroundColor Green $reportBody += "Bulk update process completed.
" } else { Write-Host "Bulk update canceled by user. No changes were made." -ForegroundColor Yellow $reportBody += "Bulk update canceled by user. No changes were made.
" } } else { Write-Host "No shared mailboxes found requiring an update (SentAs Message Copy is already enabled or mailbox property is null)." -ForegroundColor Green $reportBody += "No shared mailboxes required an update for the 'SentAs Message Copy' setting.
" } } catch { Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red # Append error details to the report body (which already has title/description) $reportBody += "An error occurred while processing shared mailbox settings.
" $reportBody += "Error Details:
$([System.Net.WebUtility]::HtmlEncode($_.Exception.Message))
Stack Trace:
$([System.Net.WebUtility]::HtmlEncode($_.Exception.StackTrace) -replace "`n", "
")
Script Line: $($_.InvocationInfo.ScriptLineNumber)
" } } finally { # --- Generate Final HTML Report --- $reportFooter = "" # Assemble the full HTML page # Use -Body for the content we built. No -PreContent needed now. ConvertTo-Html -Head $cssStyle ` -Title "Shared Mailbox Message Copy Settings Report" ` -Body $reportBody ` -PostContent $reportFooter | Out-File $outputFile -Encoding UTF8 -ErrorAction SilentlyContinue # Check if the file was actually created before trying to open it if (Test-Path $outputFile) { Write-Host "HTML Report saved to: $outputFile" Invoke-Item -Path $outputFile } else { Write-Host "Failed to create the report file at: $outputFile" -ForegroundColor Red } # Clean up Exchange session if ($session -ne $null) { Write-Host "Removing Exchange PSSession..." Remove-PSSession $session Write-Host "Session removed." } }