So the code isn’t simple, but without functions, it really isn’t that complex yet. Here is the jist of what the script does:
- Randomly determine how many regular emails to send [Lines 25 – 31]
- Randomly determine which attachment to send with attachment emails [Lines 33 – 39]
- Randomly determine which Exchange server to relay through [Lines 25 – 31]
- Randomly determine the body of the email to be sent [Lines 41 – 46]
- Store all mailboxes in a $Mailbox variable [Lines 48 – 58]
- Loop to process each mailbox as message sender
- Loop purpose 1 – send emails with no attachment [Lines 71 – 122]
- Loop purpose 1 – send emails with attachments [Lines 127- 178]
- Loop purpose 1 – send emails with a BCC recipient [Lines 184 – 239]
- Loop purpose 1 – send emails with a CC recipient [Lines 245- 300]
Here is the code below. There will be a follow-up, possibly by end of the week that will use functions, reducing the lines of code to at least in half.
Complete Script
Below is the complete code I wrote over the weekend:
##################################################################################################################### # SCRIPT DETAILS # # Sends random emails to and from every mailbox in Exchange # # # # SCRIPT VERSION HISTORY # # Current Version : 1.1 # # Change Log : 1.1 - First live testing version in a lab environment # # : 1.0 - First iteration of the script # # # # OTHER SCRIPT INFORMATION # # Wish list : functions # # : # # Rights Required : Local admin on server # # Exchange Version : 2013 * 2016 * 2019 # # Author : Damian Scoles # # My Blog : http://justaucguy.wordpress.com # # Disclaimer : You are on your own. This was not written by, supported by, or endorsed by Microsoft. # # # # # # EXECUTION # #. \RandomEmailGenerator.ps1 # # # ##################################################################################################################### # Randomizer 1 - Number of emails to send $Random1 = Get-Random -Minimum 1 -Maximum 5 If ($Random1 -eq 1) {$Number = 15} # 15 emails If ($Random1 -eq 2) {$Number = 20} # 20 emails If ($Random1 -eq 3) {$Number = 25} # 25 emails If ($Random1 -eq 4) {$Number = 30} # 30 emails If ($Random1 -eq 5) {$Number = 40} # 40 emails # Randomizer 2 - Attachments to send $Random2 = Get-Random -Minimum 1 -Maximum 5 If ($Random2 -eq 1) {$File = 'c:\SMTPGenerator\FileAttachmentOne.txt'} # 1kb File If ($Random2 -eq 2) {$File = 'c:\SMTPGenerator\FileAttachmentTwo.txt'} # 10kb File If ($Random2 -eq 3) {$File = 'c:\SMTPGenerator\FileAttachmentThree.png'} # 100kb File If ($Random2 -eq 4) {$File = 'c:\SMTPGenerator\FileAttachmentFour.txt'} # 1Mb File If ($Random2 -eq 5) {$File = 'c:\SMTPGenerator\FileAttachmentFive.jpg '} # 10Mb File # Randomizer 3 - Size of emails to send $Random3 = Get-Random -Minimum 1 -Maximum 4 If ($Random3 -eq 1) {$SMTPServer = 'EX19-EX01'} # USe EX19-EX01 to send emails If ($Random3 -eq 2) {$SMTPServer = 'EX19-EX02'} # USe EX19-EX02 to send emails If ($Random3 -eq 3) {$SMTPServer = 'EX19-EX03'} # USe EX19-EX03 to send emails If ($Random3 -eq 4) {$SMTPServer = 'EX19-EX04'} # USe EX19-EX04 to send emails # Email Bodies to be used $Body1 = Get-Content 'c:\SMTPGenerator\Body1.txt' $Body2 = Get-Content 'c:\SMTPGenerator\Body2.txt' $Body3 = Get-Content 'c:\SMTPGenerator\Body3.txt' $Body4 = Get-Content 'c:\SMTPGenerator\Body4.txt' $Body5 = Get-Content 'c:\SMTPGenerator\Body5.txt' $Body6 = Get-Content 'c:\SMTPGenerator\Body6.txt' $Body7 = Get-Content 'c:\SMTPGenerator\Body7.txt' $Body8 = Get-Content 'c:\SMTPGenerator\Body8.txt' $Body9 = Get-Content 'c:\SMTPGenerator\Body9.txt' $Body10 = Get-Content 'c:\SMTPGenerator\Body10.txt' # Other Variables $Mailboxes = Get-Mailbox | Where {$_.Alias -ne 'DiscoverySearchMailbox{D919BA05-46A6-415f-80AD-7E09334BB852}'} $MailboxCount = (Get-Mailbox | Where {$_.Alias -ne 'DiscoverySearchMailbox{D919BA05-46A6-415f-80AD-7E09334BB852}'}).Count #Send emails now Foreach ($Mailbox in $Mailboxes) { # Sender $Sender = (($Mailbox).PrimarySMTPAddress).Address # Send emails without attachments: $Num = 0 Do { # Pick Random Recipient $RandomRecipient = Get-Random -Minimum 1 -Maximum $MailboxCount $Recipient = (($Mailboxes[$RandomRecipient]).PrimarySMTPAddress).Address # Pick a Subject, Any Subject - Randomly! $Random4 = Get-Random -Minimum 1 -Maximum 10 If ($Random4 -eq 1) {$Subject = 'Wanted to send you this message'} # Random subject line 1 If ($Random4 -eq 2) {$Subject = 'Office Closed'} # Random subject line 2 If ($Random4 -eq 3) {$Subject = 'Important Notice'} # Random subject line 3 If ($Random4 -eq 4) {$Subject = 'Report is Due'} # Random subject line 4 If ($Random4 -eq 5) {$Subject = 'New client to the firm'} # Random subject line 5 If ($Random4 -eq 6) {$Subject = 'Company wide survey'} # Random subject line 6 If ($Random4 -eq 7) {$Subject = 'Random email to you'} # Random subject line 7 If ($Random4 -eq 8) {$Subject = 'Lunch plans?'} # Random subject line 8 If ($Random4 -eq 9) {$Subject = 'To do list'} # Random subject line 9 If ($Random4 -eq 10) {$Subject = 'Please read this email'} # Random subject line 10 # Choose delivery notifcation $Random5 = Get-Random -Minimum 1 -Maximum 5 # Options - None – No notification (default), OnSuccess – Notify if the delivery is successful. # OnFailure – Notify if the delivery is unsuccessful, Delay – Notify if the delivery is delayed. # Never – Never notify. If ($Random5 -eq 1) {$DeliveryNotificationOption = 'OnSuccess'} # Sets the Delivery Notification option to OnSucess If ($Random5 -eq 2) {$DeliveryNotificationOption = 'None'} # Sets the Delivery Notification option to Non If ($Random5 -eq 3) {$DeliveryNotificationOption = 'OnFailure'} # Sets the Delivery Notification option to OnFailure If ($Random5 -eq 4) {$DeliveryNotificationOption = 'Delay'} # Sets the Delivery Notification option to Delay If ($Random5 -eq 5) {$DeliveryNotificationOption = 'Never'} # Sets the Delivery Notification option to Never # Body of the email $Random6 = Get-Random -Minimum 1 -Maximum 10 If ($Random6 -eq 1) {$Body = $Body1} # Random message body 1 If ($Random6 -eq 2) {$Body = $Body2} # Random message body 2 If ($Random6 -eq 3) {$Body = $Body3} # Random message body 3 If ($Random6 -eq 4) {$Body = $Body4} # Random message body 4 If ($Random6 -eq 5) {$Body = $Body5} # Random message body 5 If ($Random6 -eq 6) {$Body = $Body6} # Random message body 6 If ($Random6 -eq 7) {$Body = $Body7} # Random message body 7 If ($Random6 -eq 8) {$Body = $Body8} # Random message body 8 If ($Random6 -eq 9) {$Body = $Body9} # Random message body 9 If ($Random6 -eq 10) {$Body = $Body10} # Random message body 10 # Send regular emails to recipients Send-MailMessage -From $Sender -To $Recipient -DeliveryNotificationOption $DeliveryNotificationOption -Subject $Subject -SMTPServer $SMTPServer -Body $Body # Increment Counter $Num++ } While ($Num -lt $Number) # Decide how many emails to send with attachments $NumAttachEmails = Get-Random -Minimum 1 -Maximum 5 # Send emails with attachments: $Num = 0 Do { # Pick Random Recipient $RandomRecipient = Get-Random -Minimum 1 -Maximum $MailboxCount $Recipient = (($Mailboxes[$RandomRecipient]).PrimarySMTPAddress).Address # Pick a Subject, Any Subject - Randomly! $Random4 = Get-Random -Minimum 1 -Maximum 10 If ($Random4 -eq 1) {$Subject = 'Wanted to send you this message'} # Random subject line 1 If ($Random4 -eq 2) {$Subject = 'Office Closed'} # Random subject line 2 If ($Random4 -eq 3) {$Subject = 'Important Notice'} # Random subject line 3 If ($Random4 -eq 4) {$Subject = 'Report is Due'} # Random subject line 4 If ($Random4 -eq 5) {$Subject = 'New client to the firm'} # Random subject line 5 If ($Random4 -eq 6) {$Subject = 'Company wide survey'} # Random subject line 6 If ($Random4 -eq 7) {$Subject = 'Random email to you'} # Random subject line 7 If ($Random4 -eq 8) {$Subject = 'Lunch plans?'} # Random subject line 8 If ($Random4 -eq 9) {$Subject = 'To do list'} # Random subject line 9 If ($Random4 -eq 10) {$Subject = 'Please read this email'} # Random subject line 10 # Choose delivery notifcation $Random5 = Get-Random -Minimum 1 -Maximum 2 # Options - None – No notification (default), OnSuccess – Notify if the delivery is successful. # OnFailure – Notify if the delivery is unsuccessful, Delay – Notify if the delivery is delayed. # Never – Never notify. If ($Random5 -eq 1) {$DeliveryNotificationOption = 'OnSuccess'} # Sets the Delivery Notification option to OnSucess If ($Random5 -eq 2) {$DeliveryNotificationOption = 'None'} # Sets the Delivery Notification option to Non If ($Random5 -eq 3) {$DeliveryNotificationOption = 'OnFailure'} # Sets the Delivery Notification option to OnFailure If ($Random5 -eq 4) {$DeliveryNotificationOption = 'Delay'} # Sets the Delivery Notification option to Delay If ($Random5 -eq 5) {$DeliveryNotificationOption = 'Never'} # Sets the Delivery Notification option to Never # Body of the email $Random6 = Get-Random -Minimum 1 -Maximum 10 If ($Random6 -eq 1) {$Body = $Body1} # Random message body 1 If ($Random6 -eq 2) {$Body = $Body2} # Random message body 2 If ($Random6 -eq 3) {$Body = $Body3} # Random message body 3 If ($Random6 -eq 4) {$Body = $Body4} # Random message body 4 If ($Random6 -eq 5) {$Body = $Body5} # Random message body 5 If ($Random6 -eq 6) {$Body = $Body6} # Random message body 6 If ($Random6 -eq 7) {$Body = $Body7} # Random message body 7 If ($Random6 -eq 8) {$Body = $Body8} # Random message body 8 If ($Random6 -eq 9) {$Body = $Body9} # Random message body 9 If ($Random6 -eq 10) {$Body = $Body10} # Random message body 10 # Send messages with an attachment, up to 5 times Send-MailMessage -From $Sender -To $Recipient -DeliveryNotificationOption $DeliveryNotificationOption -Subject $Subject -SMTPServer $SMTPServer -Body $Body -Attachments $File # Increment Counter $Num++ } While ($Num -lt $NumAttachEmails) # Decide how many emails to send with BCC $NumBCCEmails = Get-Random -Minimum 1 -Maximum 5 # Send emails with BCC: $Num = 0 Do { # Pick Random Recipient $RandomRecipient = Get-Random -Minimum 1 -Maximum $MailboxCount $Recipient = (($Mailboxes[$RandomRecipient]).PrimarySMTPAddress).Address # Pick Random BCC Recipient $RandomRecipient = Get-Random -Minimum 1 -Maximum $MailboxCount $BCCRecipient = (($Mailboxes[$RandomRecipient]).PrimarySMTPAddress).Address # Pick a Subject, Any Subject - Randomly! $Random4 = Get-Random -Minimum 1 -Maximum 10 If ($Random4 -eq 1) {$Subject = 'Wanted to send you this message'} # Random subject line 1 If ($Random4 -eq 2) {$Subject = 'Office Closed'} # Random subject line 2 If ($Random4 -eq 3) {$Subject = 'Important Notice'} # Random subject line 3 If ($Random4 -eq 4) {$Subject = 'Report is Due'} # Random subject line 4 If ($Random4 -eq 5) {$Subject = 'New client to the firm'} # Random subject line 5 If ($Random4 -eq 6) {$Subject = 'Company wide survey'} # Random subject line 6 If ($Random4 -eq 7) {$Subject = 'Random email to you'} # Random subject line 7 If ($Random4 -eq 8) {$Subject = 'Lunch plans?'} # Random subject line 8 If ($Random4 -eq 9) {$Subject = 'To do list'} # Random subject line 9 If ($Random4 -eq 10) {$Subject = 'Please read this email'} # Random subject line 10 # Choose delivery notifcation $Random5 = Get-Random -Minimum 1 -Maximum 2 # Options - None – No notification (default), OnSuccess – Notify if the delivery is successful. # OnFailure – Notify if the delivery is unsuccessful, Delay – Notify if the delivery is delayed. # Never – Never notify. If ($Random5 -eq 1) {$DeliveryNotificationOption = 'OnSuccess'} # Sets the Delivery Notification option to OnSucess If ($Random5 -eq 2) {$DeliveryNotificationOption = 'None'} # Sets the Delivery Notification option to Non If ($Random5 -eq 3) {$DeliveryNotificationOption = 'OnFailure'} # Sets the Delivery Notification option to OnFailure If ($Random5 -eq 4) {$DeliveryNotificationOption = 'Delay'} # Sets the Delivery Notification option to Delay If ($Random5 -eq 5) {$DeliveryNotificationOption = 'Never'} # Sets the Delivery Notification option to Never # Body of the email $Random6 = Get-Random -Minimum 1 -Maximum 10 If ($Random6 -eq 1) {$Body = $Body1} # Random message body 1 If ($Random6 -eq 2) {$Body = $Body2} # Random message body 2 If ($Random6 -eq 3) {$Body = $Body3} # Random message body 3 If ($Random6 -eq 4) {$Body = $Body4} # Random message body 4 If ($Random6 -eq 5) {$Body = $Body5} # Random message body 5 If ($Random6 -eq 6) {$Body = $Body6} # Random message body 6 If ($Random6 -eq 7) {$Body = $Body7} # Random message body 7 If ($Random6 -eq 8) {$Body = $Body8} # Random message body 8 If ($Random6 -eq 9) {$Body = $Body9} # Random message body 9 If ($Random6 -eq 10) {$Body = $Body10} # Random message body 10 # Send messages with an attachment, up to 5 times Send-MailMessage -From $Sender -To $Recipient -DeliveryNotificationOption $DeliveryNotificationOption -Subject $Subject -SMTPServer $SMTPServer -Body $Body -BCC $BCCRecipient # Increment Counter $Num++ } While ($Num -lt $NumBCCEmails) # Decide how many emails to send with CC $NumCCEmails = Get-Random -Minimum 1 -Maximum 5 # Send emails with BCC: $N2 = 0 Do { # Pick Random Recipient $RandomRecipient = Get-Random -Minimum 1 -Maximum $MailboxCount $Recipient = (($Mailboxes[$RandomRecipient]).PrimarySMTPAddress).Address # Pick Random CC Recipient $RandomRecipient = Get-Random -Minimum 1 -Maximum $MailboxCount $CCRecipient = (($Mailboxes[$RandomRecipient]).PrimarySMTPAddress).Address # Pick a Subject, Any Subject - Randomly! $Random4 = Get-Random -Minimum 1 -Maximum 10 If ($Random4 -eq 1) {$Subject = 'Wanted to send you this message'} # Random subject line 1 If ($Random4 -eq 2) {$Subject = 'Office Closed'} # Random subject line 2 If ($Random4 -eq 3) {$Subject = 'Important Notice'} # Random subject line 3 If ($Random4 -eq 4) {$Subject = 'Report is Due'} # Random subject line 4 If ($Random4 -eq 5) {$Subject = 'New client to the firm'} # Random subject line 5 If ($Random4 -eq 6) {$Subject = 'Company wide survey'} # Random subject line 6 If ($Random4 -eq 7) {$Subject = 'Random email to you'} # Random subject line 7 If ($Random4 -eq 8) {$Subject = 'Lunch plans?'} # Random subject line 8 If ($Random4 -eq 9) {$Subject = 'To do list'} # Random subject line 9 If ($Random4 -eq 10) {$Subject = 'Please read this email'} # Random subject line 10 # Choose delivery notifcation $Random5 = Get-Random -Minimum 1 -Maximum 2 # Options - None – No notification (default), OnSuccess – Notify if the delivery is successful. # OnFailure – Notify if the delivery is unsuccessful, Delay – Notify if the delivery is delayed. # Never – Never notify. If ($Random5 -eq 1) {$DeliveryNotificationOption = 'OnSuccess'} # Sets the Delivery Notification option to OnSucess If ($Random5 -eq 2) {$DeliveryNotificationOption = 'None'} # Sets the Delivery Notification option to Non If ($Random5 -eq 3) {$DeliveryNotificationOption = 'OnFailure'} # Sets the Delivery Notification option to OnFailure If ($Random5 -eq 4) {$DeliveryNotificationOption = 'Delay'} # Sets the Delivery Notification option to Delay If ($Random5 -eq 5) {$DeliveryNotificationOption = 'Never'} # Sets the Delivery Notification option to Never # Body of the email $Random6 = Get-Random -Minimum 1 -Maximum 10 If ($Random6 -eq 1) {$Body = $Body1} # Random message body 1 If ($Random6 -eq 2) {$Body = $Body2} # Random message body 2 If ($Random6 -eq 3) {$Body = $Body3} # Random message body 3 If ($Random6 -eq 4) {$Body = $Body4} # Random message body 4 If ($Random6 -eq 5) {$Body = $Body5} # Random message body 5 If ($Random6 -eq 6) {$Body = $Body6} # Random message body 6 If ($Random6 -eq 7) {$Body = $Body7} # Random message body 7 If ($Random6 -eq 8) {$Body = $Body8} # Random message body 8 If ($Random6 -eq 9) {$Body = $Body9} # Random message body 9 If ($Random6 -eq 10) {$Body = $Body10} # Random message body 10 # Send messages with an attachment, up to 5 times Send-MailMessage -From $Sender -To $Recipient -DeliveryNotificationOption $DeliveryNotificationOption -Subject $Subject -SMTPServer $SMTPServer -Body $Body -CC $CCRecipient # Increment Counter $Num++ } While ($Num -lt $NumCCEmails) }
Successful Runs and Their Results
Run #1
Run #2
Run #3
Running as a Scheduled Task
Since this is a lab environment and I want to generate emails, using the task scheduler to run this at night will help populate test mailboxes.
Settings
Program/Script: – C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments: -NonInteractive -WindowStyle Hidden -command “. ‘C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1’; Connect-ExchangeServer -auto; C:\SMTPGenerator\RandomEmailGenerator.ps1”
Run even when no logged in: Checked
Run at highest privileges: Checked