Site icon Franky's Web

Exchange 2010/2013: Monitor mailbox for unread messages (EWS)

This little script monitors the inbox of a mailbox for unread messages. I created it to monitor service mailboxes from which other programs automatically process mails. With a few small adjustments, it can also be used to create a monitor for PRTG or Solarwinds.

#-----------------------------
# Check mailbox for unread mails
# www.frankysweb.de
#
# by Frank Zöchling
# Version 0.1
#
#-----------------------------

$mailboxName = „journal@frankysweb.de“ #zu überprüfende Mailbox
$maxUnreadAge = „20“ #Wert in Minuten (1 Tag = 1440 Minuten)

#EWS Establish connection

Add-Type -Path „C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll“
$version = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService($version)
$service.UseDefaultCredentials = $true
$service.AutodiscoverUrl($mailboxName)

$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

#Mails abrufen und auswerten

$today = get-date
$maxagetime = $today.AddMinutes(-$maxUnreadAge)

$mails = $inbox.FindItems(100)

$unreadmails = $mails | where {$_.isRead -match „False“}
$unreadmailscount = $unreadmails | measure-object | foreach-object {$_.Count}
$unreadmailsmaxagecount = $unreadmails | where {$_.DateTimeReceived -lt $maxagetime} | measure-object | foreach-object {$_.Count}

write-host „Anzahl ungelesene E-Mails: $unreadmailscount“
write-host „Anzahl ungelesene E-Mails älter als $maxUnreadAge Minuten: $unreadmailsmaxagecount“

To run the script the EWS API 2.0 is required, the API can be downloaded here:

http://www.microsoft.com/en-us/download/details.aspx?id=35371

Exit mobile version