try { # Import admin credentials $serverAddress = Get-Content -Path ".\DefaultServer.txt" $credential = Import-Clixml -Path .\AdminCredential.xml # 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 Import-PSSession $session -DisableNameChecking -WarningAction SilentlyContinue -ErrorAction Stop Write-Host "Successfully connected." # Define output file path in the user's Downloads folder $outputFile = "$env:USERPROFILE\Downloads\ExchangeDatabaseMailboxListReport.html" # --- Data Collection Variables --- $totalUserMailboxes = 0 $totalSharedMailboxes = 0 $databaseStats = @() # Array to hold stats for the column chart # --- HTML Report Setup --- # Define CSS styles and include Chart.js library via CDN $head = @" "@ # Start building the HTML body content $bodyContent = "
Generated on: $(Get-Date)
" $bodyContent += "Note: System mailboxes such as 'Discovery Search Mailbox' have been excluded from this report.
" # Get database size and mailboxes Write-Host "Retrieving database information..." $databases = Get-MailboxDatabase -Status | Select-Object Name, DatabaseSize Write-Host "Found $($databases.Count) databases." # --- Process Each Database --- # (Loop remains the same) foreach ($db in $databases) { Write-Host "Processing database: $($db.Name)" $dbName = $db.Name $dbSize = $db.DatabaseSize -replace '\s*\(.*\)', '' # Clean up size string # Add database details to the HTML body (these will now be centered by CSS) $bodyContent += "Total Size: $dbSize
" # Retrieve mailboxes under the database and exclude "Discovery Search Mailbox" Write-Host " Retrieving mailboxes for $dbName..." $mailboxes = Get-Mailbox -Database $dbName | Where-Object {$_.DisplayName -ne "Discovery Search Mailbox"} | Select-Object DisplayName, PrimarySmtpAddress, RecipientTypeDetails # Separate user mailboxes and shared mailboxes $userMailboxes = $mailboxes | Where-Object {$_.RecipientTypeDetails -eq "UserMailbox"} | Sort-Object DisplayName | Select-Object DisplayName, PrimarySmtpAddress $sharedMailboxes = $mailboxes | Where-Object {$_.RecipientTypeDetails -eq "SharedMailbox"} | Sort-Object DisplayName | Select-Object DisplayName, PrimarySmtpAddress # Accumulate Counts $currentUserCount = $userMailboxes.Count $currentSharedCount = $sharedMailboxes.Count $totalUserMailboxes += $currentUserCount $totalSharedMailboxes += $currentSharedCount $databaseStats += [PSCustomObject]@{ DatabaseName = $dbName UserCount = $currentUserCount SharedCount = $currentSharedCount } # Add User Mailboxes Table $bodyContent += "Count: $currentUserCount
" } else { $bodyContent += "No user mailboxes found in this database.
" } # Add Shared Mailboxes Table $bodyContent += "Count: $currentSharedCount
" } else { $bodyContent += "No shared mailboxes found in this database.
" } Write-Host " Finished processing $dbName." } # End foreach database # --- Add Chart Placeholders and Script --- $bodyContent += "