#==============================================================================================================
#Created by: Taylour Blackwell and Brian Zoucha
# Filename: O365MailFlow_v6.ps1
# Based on: DIY O365 Monitoring by Rob Rinear
# Description: This will simulate a user sending an email from one O365 account to another O365 accou and then verify that the email was received.
# O365 --> O365
# Updates: V2- Updated 7/11/17- removed old unneeded code, Updated the log entries to include identification of sender and reciever accounts
# V3- Updated script to use variable for EWS URL
# V3.5- Updated 7/28/17- Turned the sleep integer into a variable so that it can be modified as an overidable parameter
# V4- Updated 8/2/17- Added sender and Receiver accounts to property bag.
# V5- Updated 9/19/17-TB- Adding stopwatch to allow for timing duration of scripts
# v5.5- Updated 1/29/18-IA- Ismaen Aboubakare added error checking to the script.
# TB- added comments and added the error checking to the property bag.
# Updated 8/15/18-TB-Fixed issue with total time not adding correctly
# v6- Updated 10/31/18 -TB- Added integer for stopwatch to add to the property bag for the performance rule
#==============================================================================================================
#listing Parameters
Param(
[parameter(Mandatory=$true)]
$SenderUser,
[parameter(Mandatory=$true)]
$ReceiverUser,
[parameter(Mandatory=$true)]
$SenderPassword,
[parameter(Mandatory=$true)]
$ReceiverPassword,
[parameter(Mandatory=$true)]
$O365URL,
[parameter(Mandatory=$true)]
$SleepINT
)
#--- Set Stopwatch to measure activity time of sending email ---#
$swsd = New-Object Diagnostics.Stopwatch
$swsd.Start()
#--- Establish connection to O365 as Sender ---#
#---Try/Catch block attempts to connect ro O365 as sender user and outputs on error---#
try{
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2010_SP1
$service.Url="$O365URL"
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $SenderUser, $SenderPassword
}
catch{
$connectionError = 'Problem with establishing connection to O365. '
}
#--- Generate Test email from sender to receiver ---#
#---Try/Catch block attempts to send email and outputs on error---#
try{
$TestID = (get-date -format MMddyyyyHHmmss) + "-" + (get-random)
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
$message.Subject = 'Test Message ' + $TestID + '40'
$message.Body = 'This message is being sent through SCOM using EWS'
$message.ToRecipients.Add($ReceiverUser)
$message.SendAndSaveCopy()
}
catch{
$emailError = 'Problem with sending email from Sender to Receiver. '
}
#--- Stop Stopwatch and record duration of sending email ---#
$swsd.Stop()
#--- Converts Stopwatch results to String ---#
$swsd.Elapsed.toString()
$SWSDTime = $swsd.Elapsed.toString()
#--- Converts Stopwatch results to Integer in Milliseconds ---#
$swsd.elapsed.TotalMilliseconds
$SWSDTimeMil = $swsd.elapsed.TotalMilliseconds
#--- Set Stopwatch to measure activity time of retreival ---#
$swrd = New-Object Diagnostics.Stopwatch
$swrd.Start()
#--- Establish connection to Receiver Inbox ---#
#---Try/Catch block attempts to connect ro O365 as receiver user and outputs on error---#
try{
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $ReceiverUser, $ReceiverPassword
$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)
}
catch{
$receiveInboxError = 'Cannot establish Connection to Receiver. '
}
#--- Search last 100 emails for one matching the test sent above ---#
#--- Will sleep the number of seconds set in $Sleepint between attempts if not found to account for delay ---#
#--- Will exit after 10 attempts ---#
#--- Updated search string 4/4/17 and 8/2/17 ---#
$SearchString="System.Subject:" + $TestID + '40'
$i = 0
do {sleep $Sleepint; $EmailResponse=$Inbox.FindItems($SearchString,100); $i++}
until (($EmailResponse.count -gt 0) -or ($i -gt 10))
#--- Stop Stopwatch and record duration of Message retreival ---#
$swrd.Stop()
#--- Converts Stopwatch results to String ---#
$swrd.Elapsed.toString()
$SWRDTime = $swrd.Elapsed.toint()
#--- Converts Stopwatch results to Integer in Milliseconds ---#
$swrd.elapsed.TotalMilliseconds
$SWRDTimeMil = $swrd.elapsed.TotalMilliseconds
#--- Adds all stop watches together to string---#
$TotalDuration = $SWRD.Elapsed.Add($SWSD.Elapsed).ToString()
#--- Adds all stop watches together to Integer---#
$TotalDurationMil = $SWRD.Elapsed.Add($SWSD.Elapsed).totalmilliseconds
#--- Did we find the response or exit after 10 attempts? ---#
if ( $i -gt 10 ) {
$bag.AddValue('State',"Critical")
$bag.AddValue('Sender Account',"$SenderUser")
$bag.AddValue('Receiver Account',"$ReceiverUser")
$bag.AddValue('Message Send Duration in ms',"$SWSDTimeMil")
$bag.AddValue('Message Receive Duration in ms',"$SWRDTimeMil")
$bag.AddValue('Total Duration in ms',"$TotalDurationMil")
$api.LogScriptEvent('HealthService',40,1,"O365MailFlow ERROR: Email NOT received for test ID $TestID. $connectionError $emailError $receiveInboxError Sender: $SenderUser Receiver: $ReceiverUser Send Duration: $SWSDTime Receive Duration: $SWRDTime TotalDuration: $TotalDuration State: Critical")
} else {
#$EmailResponse
$bag.AddValue('State',"Healthy")
$bag.AddValue('Sender Account',"$SenderUser")
$bag.AddValue('Receiver Account',"$ReceiverUser")
$bag.AddValue('Message Send Duration in ms',"$SWSDTimeMil")
$bag.AddValue('Message Receive Duration in ms',"$SWRDTimeMil")
$bag.AddValue('Total Duration in ms',"$TotalDurationMil")
$api.LogScriptEvent('HealthService',40,0,"O365MailFlow SUCCESS: Mail flow completed for test ID $TestID. Sender: $SenderUser Receiver: $ReceiverUser Send Duration: $SWSDTime Receive Duration: $SWRDTime TotalDuration: $TotalDuration State: Healthy")
#$EmailResponse.Delete('HardDelete')