Email Traffic Simulator using PowerShell : Exchange 2019 / SE

 



🚀 Building a Hybrid Exchange Email Traffic Simulator using PowerShell

🔷 Introduction

In modern enterprise environments, hybrid Exchange deployments combining Exchange 2019 (on‑premises) and Exchange Online are very common.

One of the major challenges in such environments is validating real‑world mail flow across:

  • On‑prem mailboxes
  • Exchange Online mailboxes
  • Hybrid connectors
  • Transport rules
  • Attachments flow

While native tools help diagnose issues, they don’t simulate realistic user traffic.

To solve this, I built a PowerShell-based email traffic generator that simulates real enterprise email flow.


🔷 Problem Statement

While working in a hybrid environment, I encountered the following challenges:

  • Get-Mailbox returns system and hidden mailboxes
  • No built-in way to simulate bulk user traffic
  • Need to include attachments randomly
  • Need to validate hybrid mail flow (OnPrem ↔ EXO)
  • SMTP relay must be secure and not an open relay

🔷 Solution Overview

To address these challenges, I built a script that:

  • Sends emails across real user mailboxes
  • Excludes system, discovery, and arbitration mailboxes
  • Uses a secure SMTP relay connector (custom port)
  • Randomly selects recipients and attachments
  • Adds realistic delays between emails
  • Displays progress using a live progress bar

🔷 SMTP Relay Design

Instead of modifying the default receive connector (which can break production mail flow), I created a dedicated relay connector with:

  • Port: 2526
  • IP restriction (only the script machine allowed)
  • Anonymous relay enabled

This ensures:

  • No open relay risk
  • No impact to production or hybrid routing
  • Clean separation of test traffic

🔷 Mailbox Filtering Strategy

One key improvement was ensuring only valid user mailboxes are used.

On-prem filtering:

  • UserMailbox only
  • Excludes hidden mailboxes

Remote (Exchange Online via RemoteMailbox):

  • Filters valid EXO mailboxes
  • Excludes system or incomplete objects

This avoids:

  • Arbitration mailboxes
  • Discovery mailboxes
  • Health mailboxes
  • System mailboxes

🔷 Full Script (Clean Copy-Paste Version)

Below is the full working script:

$SMTPServer = "SERVERNAME"
$SMTPPort = 2526
$Sender = "testmailer@cloudmonkeys.xyz"
$AttachmentFolder = "C:\TestAttachments"

$TotalMessages = 50
$MaxRecipients = 5
$DelayMin = 2
$DelayMax = 6
$CheckpointInterval = 10

Write-Host "Fetching On-Prem user mailboxes..."
$onPremMailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox -Filter {HiddenFromAddressListsEnabled -eq $false} | Select-Object -ExpandProperty PrimarySmtpAddress

Write-Host "Fetching Exchange Online user mailboxes (Remote)..."
$remoteMailboxes = Get-RemoteMailbox -ResultSize Unlimited | Where-Object {$.PrimarySmtpAddress -ne $null -and $.HiddenFromAddressListsEnabled -eq $false -and $_.RemoteRecipientType -match "ProvisionMailbox"} | Select-Object -ExpandProperty PrimarySmtpAddress

$mailboxes = ($onPremMailboxes + $remoteMailboxes) | Sort-Object -Unique

Write-Host "Total usable mailboxes:" $mailboxes.Count

if ($mailboxes.Count -lt 2) { Write-Host "Not enough valid user mailboxes!" -ForegroundColor Red; exit }

$attachments = Get-ChildItem $AttachmentFolder -ErrorAction SilentlyContinue

if (!$attachments) { Write-Host "No attachments found - emails will be sent without attachments" }

$subjects = @("Project Update","Weekly Status","Action Required","Meeting Notes","Hybrid Traffic Simulation")
$bodies = @("Please review.","Sharing for reference.","Let me know your thoughts.","Hybrid mail flow validation.","No action required.")

for ($i = 1; $i -le $TotalMessages; $i++) {

$percent = ($i / $TotalMessages) * 100  
Write-Progress -Activity "Sending Hybrid Test Emails" -Status "Sending $i of $TotalMessages" -PercentComplete $percent  

$recipientCount = Get-Random -Minimum 1 -Maximum $MaxRecipients  
$recipients = Get-Random -InputObject $mailboxes -Count $recipientCount  
$recipients = $recipients | Where-Object { $_ -ne $Sender }  

$subject = "HybridTest-" + (Get-Random -Minimum 10000 -Maximum 99999) + " | " + (Get-Random -InputObject $subjects)  
$body = Get-Random -InputObject $bodies  

$addAttachment = (Get-Random -Minimum 1 -Maximum 10) -le 4  

try {  

    if ($addAttachment -and $attachments.Count -gt 0) {  
        $maxAttach = :Min(2, $attachments.Count)  
        $attachmentCount = Get-Random -Minimum 1 -Maximum ($maxAttach + 1)  
        $selectedAttachments = Get-Random -InputObject $attachments -Count $attachmentCount  

        Send-MailMessage -From $Sender -To $recipients -Subject $subject -Body $body -Attachments $selectedAttachments.FullName -SmtpServer $SMTPServer -Port $SMTPPort  
    }  
    else {  
        Send-MailMessage -From $Sender -To $recipients -Subject $subject -Body $body -SmtpServer $SMTPServer -Port $SMTPPort  
    }  

    Write-Host "[$i] Sent → $($recipients -join ', ')" -ForegroundColor Green  

} catch {  
    Write-Host "Error [$i]: $_" -ForegroundColor Red  
}  

if ($i % $CheckpointInterval -eq 0) {  
    Write-Host "✅ Progress: $i / $TotalMessages emails sent" -ForegroundColor Yellow  
}  

Start-Sleep -Seconds (Get-Random -Minimum $DelayMin -Maximum $DelayMax)  

}

Write-Progress -Activity "Sending Hybrid Test Emails" -Completed
Write-Host "✅ Hybrid email simulation completed successfully!"


🔷 How the Script Works

Mailbox Discovery

Fetches:

  • On-prem user mailboxes
  • Exchange Online mailboxes via RemoteMailbox

Traffic Simulation

  • Random recipients (1 to 5 users)
  • Random subjects and bodies
  • Random attachment inclusion (~40%)

Hybrid Routing

Exchange handles routing automatically:

  • OnPrem → OnPrem
  • OnPrem → Exchange Online (via hybrid connector)

Progress Monitoring

  • Live progress bar
  • Checkpoint logs after every batch

🔷 Validation

After running the script, validate using:

Get-MessageTrackingLog -Start (Get-Date).AddMinutes(-30) | Where {$_.MessageSubject -like "HybridTest-*"}

Look for:

  • RECEIVE → mail accepted
  • SEND → internal routing
  • SENDEXTERNAL → Exchange Online

🔷 Final Outcome

With this approach, you now have:

  • Secure SMTP relay setup
  • Realistic user traffic simulation
  • Clean mailbox targeting
  • Full hybrid mail flow validation
  • Scalable test framework

🔷 Conclusion

This solution turns basic mail testing into a realistic enterprise simulation tool.

It helps in:

  • Hybrid troubleshooting
  • Load testing
  • Transport rule validation
  • Attachment flow checks


Comments