# Define path for HTML report $outputFile = "$env:USERPROFILE\Downloads\UserMailboxMessageCopySettingsReport(Navitas).html" # Changed filename # Define path for server address and credentials $serverAddressFile = ".\DefaultServer.txt" $credentialFile = ".\AdminCredential.xml" # --- HTML Styling (CSS) --- # (CSS Style remains the same as before) $cssStyle = @" "@ # --- Script Logic --- $session = $null # Initialize session variable # --- Initialize Report Body with Title and Descriptions --- # Start with the main H1 title for User Mailboxes $reportBody = "

User Mailbox Message Copy Settings Report

" # Updated Title # Define and add the descriptions right after the title - updated for User Mailboxes $descriptionHtml = @"

Understanding Message Copy Settings for User Mailboxes:

These settings apply when delegation permissions ('Send As' or 'Send on Behalf') are granted to allow one user to send email from another user's mailbox.

Note: This script is for reporting purposes only and does not modify any settings.

"@ $reportBody += $descriptionHtml try { # --- Input Validation --- if (-not (Test-Path $serverAddressFile)) { throw "Server address file not found: $serverAddressFile" } if (-not (Test-Path $credentialFile)) { throw "Credential file not found: $credentialFile" } # Import admin credentials $serverAddress = Get-Content -Path $serverAddressFile $credential = Import-Clixml -Path $credentialFile # Establish session with Exchange Server Write-Host "Connecting to Exchange Server: $serverAddress..." $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://$serverAddress/PowerShell/" -Authentication Kerberos -Credential $credential -ErrorAction Stop Import-PSSession $session -DisableNameChecking -ErrorAction Stop Write-Host "Successfully connected to Exchange." -ForegroundColor Green # --- Data Retrieval for USER Mailboxes --- Write-Host "Retrieving USER mailboxes..." # *** Changed RecipientTypeDetails to UserMailbox *** $mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited | Select-Object DisplayName, PrimarySmtpAddress, MessageCopyForSendOnBehalfEnabled, MessageCopyForSentAsEnabled -ErrorAction Stop Write-Host "Found $($mailboxes.Count) user mailboxes." # --- Generate User Mailbox Report Section --- # *** Changed H2 title slightly *** $reportBody += "

User Mailbox Message Copy Settings

" if ($mailboxes.Count -gt 0) { # Format boolean values for better HTML display $formattedMailboxes = $mailboxes | Select-Object DisplayName, PrimarySmtpAddress, @{Name='SendOnBehalfEnabled';Expression={$_.MessageCopyForSendOnBehalfEnabled}}, @{Name='SentAsEnabled';Expression={$_.MessageCopyForSentAsEnabled}} # Generate the HTML fragment (it will be encoded by default) $htmlFragment = $formattedMailboxes | ConvertTo-Html -Fragment -Property DisplayName, PrimarySmtpAddress, @{Label='SendOnBehalf Message Copy'; Expression = {"$($_.SendOnBehalfEnabled)"}}, @{Label='SentAs Message Copy'; Expression = {"$($_.SentAsEnabled)"}} # Decode the HTML fragment before adding it to the report body $reportBody += [System.Net.WebUtility]::HtmlDecode($htmlFragment) } else { $reportBody += "

No user mailboxes found.

" # Updated message } # --- Update Logic and Sections REMOVED --- # No filtering, prompting, Set-Mailbox, or "AFTER" report needed. $reportBody += "

Report generation complete.

" # Simple completion message } catch { Write-Host "An error occurred: $($_.Exception.Message)" -ForegroundColor Red # Append error details to the report body (which already has title/description) $reportBody += "

Script Execution Error

" $reportBody += "

An error occurred while retrieving user mailbox settings.

" # Updated error context $reportBody += "

Error Details:
$([System.Net.WebUtility]::HtmlEncode($_.Exception.Message))

" if ($_.Exception.StackTrace) { $reportBody += "

Stack Trace:
$([System.Net.WebUtility]::HtmlEncode($_.Exception.StackTrace) -replace "`n", "
")

" } if ($_.InvocationInfo) { $reportBody += "

Script Line: $($_.InvocationInfo.ScriptLineNumber)

" } } finally { # --- Generate Final HTML Report --- $reportFooter = "

Report generated on: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') by $($env:USERNAME)

" # Assemble the full HTML page # *** Updated Title parameter *** ConvertTo-Html -Head $cssStyle ` -Title "User 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." } }