O365SMP O365toO365 MailFlow Monitor ProbeActionModuleType

O365SMP.O365toO365.MailFlow.Monitor.ProbeActionModuleType (ProbeActionModuleType)

Probe Action Module Type for Unit Monitor: O365SMP.O365toO365.MailFlow.Monitor

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
OutputTypeSystem.PropertyBagData

Member Modules:

ID Module Type TypeId RunAs 
Probe ProbeAction Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
TimeoutSecondsint$Config/TimeoutSeconds$Timeout Seconds
SenderUserstring$Config/SenderUser$Sender User
ReceiverUserstring$Config/ReceiverUser$Receiver User
SenderPasswordstring$Config/SenderPassword$Sender Password
ReceiverPasswordstring$Config/ReceiverPassword$Receiver Password
O365URLstring$Config/O365URL$O365 URL
SleepINTint$Config/SleepINT$Sleep INT

Source Code:

<ProbeActionModuleType ID="O365SMP.O365toO365.MailFlow.Monitor.ProbeActionModuleType" Accessibility="Internal" Batching="false" PassThrough="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SenderUser" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ReceiverUser" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SenderPassword" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ReceiverPassword" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="O365URL" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SleepINT" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
<OverrideableParameter ID="SenderUser" Selector="$Config/SenderUser$" ParameterType="string"/>
<OverrideableParameter ID="ReceiverUser" Selector="$Config/ReceiverUser$" ParameterType="string"/>
<OverrideableParameter ID="SenderPassword" Selector="$Config/SenderPassword$" ParameterType="string"/>
<OverrideableParameter ID="ReceiverPassword" Selector="$Config/ReceiverPassword$" ParameterType="string"/>
<OverrideableParameter ID="O365URL" Selector="$Config/O365URL$" ParameterType="string"/>
<OverrideableParameter ID="SleepINT" Selector="$Config/SleepINT$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<ProbeAction ID="Probe" TypeID="Windows!Microsoft.Windows.PowerShellPropertyBagTriggerOnlyProbe">
<ScriptName>O365MailFlow_v4.ps1</ScriptName>
<ScriptBody><Script>

#==============================================================================================================
#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 --&gt; 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
)

#listing Variables
$api = new-object -comObject 'MOM.ScriptAPI'
$bag = $api.CreatePropertyBag()

Add-Type -Path 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'

#--- 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')

}


$bag


</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>SenderUser</Name>
<Value>$RunAs[Name="O365SMP.O365.Sender.Profile"]/UserName$</Value>
</Parameter>
<Parameter>
<Name>ReceiverUser</Name>
<Value>$RunAs[Name="O365SMP.O365.Receiver.Profile"]/UserName$</Value>
</Parameter>
<Parameter>
<Name>SenderPassword</Name>
<Value>$RunAs[Name="O365SMP.O365.Sender.Profile"]/Password$</Value>
</Parameter>
<Parameter>
<Name>ReceiverPassword</Name>
<Value>$RunAs[Name="O365SMP.O365.Receiver.Profile"]/Password$</Value>
</Parameter>
<Parameter>
<Name>O365URL</Name>
<Value>$Config/O365URL$</Value>
</Parameter>
<Parameter>
<Name>SleepINT</Name>
<Value>$Config/SleepINT$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
<StrictErrorHandling>false</StrictErrorHandling>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="Probe"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.PropertyBagData</OutputType>
<TriggerOnly>true</TriggerOnly>
</ProbeActionModuleType>