CW Manage – Monitoring the EmailRobot Service

Quinntin Comer Uncategorized 1 Comment

A common issue that we hear about, and experience, is that the EmailRobot service is no longer processing items in the Inbox of your email connector mailbox. Typically, this is solved by a service restart. With a little bit of PowerShell magic – you can monitor the inbox and restart the services if certain conditions are met.

So, we have to do a few things to get ready for this.

Pre-reqs

  • Download and install the EWS API 2.1 (in the script, we are referencing the 32 bit version)
  • Generate a secure string password file

The Steps

Generate an Encrypted Password File

(get-credential).password | ConvertFrom-SecureString | set-content "C:\CWPassword.txt"

The above script will generate a file that contains an encrypted version of the password. Now that we have it there, we can reference that in our script.

# Credentials
$username = 'EmailAddress'
$password= Get-Content "C:\CWpassword.txt" | ConvertTo-SecureString

# load the assembly
if ($(Test-Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll") -eq $False){
 Write-Host "Requires Exchange Web Services Managed API 2.1 be installed on computer running script."
 EXIT 1
}
[void] [Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll")

$s = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$s.Credentials = New-Object Net.NetworkCredential($username, $password)
$s.Url='https://outlook.office365.com/EWS/Exchange.asmx'

# get a handle to the inbox
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($s,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

#create a property set (to let us access the body & other details not available from the FindItems call)
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropertySet.RequestedBodyType = [Microsoft.Exchange.WebServices.Data.BodyType]::Text;

$items = $inbox.FindItems(100)
if ($inbox.TotalCount -ne "0") {

 foreach ($item in $items.Items) {
 #load the property set
 $item.load($psPropertySet)
 
 if ($item.DateTimeCreated -lt (Get-Date).AddMinutes(-10)) {
 Write-Host "Messages older than 10 min. Restarting EmailRobot Service"
 $service = Get-Service -Name 'EmailRobot'
 $service.Stop()
 $service.WaitForStatus('Stopped','00:00:10')
 if ($service.Status -ne 'Stopped') {Get-Process -Name 'emailrobot' | Stop-Process -Force}
 Start-Service "EmailRobot"
 Write-Host "Services restarted."
 EXIT
 }
 Else {Write-Host "Email less than 10min old. $($item.DateTimeCreated)"}
 }
}
Else {Write-Host "There are no messages in the mailbox"}

Now you will want to create a scheduled task in Windows and run this every 5 minutes or so. To test, try killing the EmailRobot service and verify it works properly.

Coming up soon: Adding ticket creation with API if there is a failure.

Comments 1

  1. I actually wrote a similar powershell script on my own that works very similarly but incorporates one thing that you may want to consider…
    In my version, rather than encoding and storing the password for the email connector, I encode and store a password for a read-only database user for the connectwise database… Then you can simply query the database for the list of email connectors to monitor including the usernames/passwords that Connectwise actually uses. That way if you have multiple email connectors, you can manage all of them from one script.

    Query that I use to pull that info(could be modified to pull the server name as well, but I didn’t need that).
    select Name, Username, [dbo].[udf_DecrText](Password) as passwd From [dbo].[Exchange_Robot_Imap_Server]

Leave a Reply

Your email address will not be published. Required fields are marked *