Link to home
Start Free TrialLog in
Avatar of rakkad
rakkadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell script to check e-mails and report

Hi

Can anyone in assist me in doing the following:-

1. Check the presence of an e-mail e.g. Cognos Start Success Message, if not found within a time period e.g. 4hr, report to a log file as Cognos Start Success Message not found.  If found then look for an e-mail 'Cognos Finish Success Message, if not found within a time period e.g. 5hrs report to a log file as Cognos Finish Success Message not found.
Avatar of Joe Klimis
Joe Klimis
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi

I have created the following script for you

it can be run like this  
.\ews-check-mail.ps1 -message1 "Cognos Start Success" -message2 "Cognos finish Success" -mailbox "my,mail@microsoft.com" -smtpServerName "smtp.mymailServer.com"

Open in new window


The script checks the subject to see if it contains message 1 wait 1 hr and check for 3 hrs is message2 arrives . creating a log file myLogFile.txt


# https://www.experts-exchange.com/questions/29041318/Powershell-script-to-check-e-mails-and-report.html


param(
Cognos Start Success        = "Joe.blogs@microsoft.com",
$smtpServerName = "smtp.mymailServer.com",
$message1 = "Cognos Start Success",
$message2 = "Cognos Finish Success",
$logfile = "myLogFile.txt" 
)


# https://seanonit.wordpress.com/2014/10/29/using-powershell-and-ews-to-monitor-a-mailbox/

# download  Https://www.microsoft.com/en-gb/download/details.aspx?id=42951
#  See the EWS Managed API reference at: http://msdn.microsoft.com/en-us/library/jj220535(v=exchg.80).aspx

#  C:\Program Files\Microsoft\Exchange\Web Services\2.2\

# Load the EWS Managed API

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


function get-emailmessages {
param(
$mailbox        = "joe.klimis@interoute.com",
$smtpServerName = "hostmaster.interoute.com",
$minutes=240
)
  $Exchange2007SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1
  $Exchange2010    = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010
  $Exchange2010SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
  $Exchange2010SP2 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
  $Exchange2013    = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013
  $Exchange2013SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1
 
  # create EWS Service object for the target mailbox name
  $exchangeService = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList $Exchange2010SP2
  $exchangeService.UseDefaultCredentials = $true
  $exchangeService.AutodiscoverUrl($mailbox)
 
  # bind to the Inbox folder of the target mailbox
  $inboxFolderName = [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox
  $inboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchangeService,$inboxFolderName)
 
  # Optional: reduce the query overhead by viewing the inbox 100 items at a time
  $itemView = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList 100
  # search the mailbox for messages older than variable $minutes minutes
  $dateTimeItem = [Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived
  $300MinutesAgo = (Get-Date).AddMinutes(-$minutes)
  $searchFilter = New-Object -TypeName Microsoft.Exchange.WebServices.Data.SearchFilter+IsLessThanOrEqualTo -ArgumentList $dateTimeItem,$300MinutesAgo
  $foundItems = $exchangeService.FindItems($inboxFolder.Id,$searchFilter,$itemView)
  return $founditems
}


 try {
 
    $items  = get-emailmessages -mailbox $mailbox  -smtpServerName $smtpServerName | where  { $_.subject -match $message1} 

  
	if ($items.count -ge 1)  
	{
	 #$foundItems | where  { $_.subject -match $subjectmatch }
		
		$count=1
		while ($count -le 4)
		{
		 sleep -seconds (60*60*1) #  wait 1hr 
		 $Finshed  = get-emailmessages -mailbox $mailbox  -smtpServerName $smtpServerName -minutes 300| where  { $_.subject -match $message2} 
		 $count++
		 if  ($finished ) { break }
		}
		"$(get-date) $message2 NOT found in 300 Minutes" | out-file -append $logfile
	} 
	Else { 
		"$(get-date) $message1 NOT found in 240 Minutes" | out-file -append $logfile}   
	  }
	catch
	{
	  $messageBody = "{0}`r`n{1}" -f $_.Exception.Message,$_.InvocationInfo.PositionMessage
	  Write-output $messageBody
	}

Open in new window

Avatar of rakkad

ASKER

Thanks for this.  I would like explaination of the code please, as I am new to the some of the concepts listed.

Thank-you
ASKER CERTIFIED SOLUTION
Avatar of Joe Klimis
Joe Klimis
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rakkad

ASKER

Thanks - much helpful