Skip to main content

Digging a little deeper to look to see if a mailbox is being used with the EWS Managed API and Powershell

Change is one of the universal constants that we all must constantly deal within our working lives, another one is poor communication and dysfunctions HR departments bit like death and tax's really. This often leads to us hapless mail administrator wondering why when company staff numbers remain stable the number of mailboxes seem to grow exponentially over time. Those that seek to find unused mailboxes are often confronted with a somewhat challenging task because of the nature of the challenging environment we work in for example sickness, maternity (and paternity leave), gap years and a number of other flexible work scenarios where mailboxes may appear to be unused but should not be deleted.

Over the last decade I've had a few cracks at writing scripts to do this at first just looking at the number of unread email, the looking at both send and received and then finally looking at all of these and then the last logon time while all shared some modicum of success at time they fell a little short of optimal. So for the year of the Tiger here's another method that goes along the logic line of old saying is that you could never have to much information. So what this script does is use EWS to pull the following bit of information.

First the number of Unread email in the last 365 days

While seeming logical the fact is a lot of users leave unread email in their mailboxes for a large number of inexplicable reasons. Mostly an overinflated sense of important-ness so if you delete these mailboxes finding a new job maybe the least of your worries.

Last Unread Email DateTime

Combined with the first bit of information this can provide a more useful snip it allowing you see if a mailbox is at least actively receiving mail.

Last Read Email DateTime

Now when you combine this with the first two is where you really get something useful because if you know when the last time someone read an email you pretty much now when that mailbox was last used (elemental really).

Subjects for the following

This funnily enough can also provide a good detail of insight into if a mailboxes is being used eg if the last email some receive is goodbye, goodluck etc you can make a pretty good assumption if that mailbox is being used.

Last Sent DateTime and Subject

Again another good indicator is the last email someone sent was a greener pastures email then it should be good for pruning.

Last DateTime an Appointment of Contact was created

Because mailboxes aren't always just used for email these are two things that could save you bacon if you looking at deleting a mailbox.

How does it work , it basically combine a number of different queries of a mailbox to extract the information i've put a download of this code here the script itself look like.

$MailboxName = $args[0]
$MailDate = [system.DateTime]::Now.AddDays(-365)

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)

$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://"
$aceuser = [ADSI]$sidbind

$service.AutodiscoverUrl($aceuser.mail.ToString())

$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
$InboxFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$Sfir = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::IsRead, $false)
$Sflt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $MailDate)
$sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
$sfCollection.add($Sfir)
$sfCollection.add($Sflt)
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(20000)
$frFolderResult = $InboxFolder.FindItems($sfCollection,$view)
"Number of Unread Email for the Last 365 Days : " + $frFolderResult.Items.Count
if ($frFolderResult.Items.Count -ne 0){
"Last Unread Subject : " + $frFolderResult.Items[0].Subject
"Last Unread DateTime : " + $frFolderResult.Items[0].DateTimeReceived
}
$view = new-object Microsoft.Exchange.WebServices.Data.ItemView(1)
$frFolderResult = $InboxFolder.FindItems($view)
if ($frFolderResult.Items.Count -ne 0){
"Last Recieved Subject : " + $frFolderResult.Items[0].Subject
"Last Recieved DateTime : " + $frFolderResult.Items[0].DateTimeReceived
}
$sfview = new-object Microsoft.Exchange.WebServices.Data.ItemView(1)
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::SentItems,$MailboxName)
$SentItemsFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$srFolderResult = $SentItemsFolder.FindItems($sfview)
if ($srFolderResult.Items.Count -ne 0){
"Last Sent Subject : " + $srFolderResult.Items[0].Subject
"Last Sent DateTime : " + $srFolderResult.Items[0].DateTimeReceived
}
$cfview = new-object Microsoft.Exchange.WebServices.Data.ItemView(1)
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Contacts,$MailboxName)
$ContactsFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$cfFolderResult = $ContactsFolder.FindItems($sfview)
if ($srFolderResult.Items.Count -ne 0){
"Last Contact Created: " + $cfFolderResult.Items[0].Subject
"Last Contact CreatedTime : " + $cfFolderResult.Items[0].DateTimeReceived
}
$apview = new-object Microsoft.Exchange.WebServices.Data.ItemView(1)
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName)
$CalendarFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
$cfFolderResult = $CalendarFolder.FindItems($apview)
if ($srFolderResult.Items.Count -ne 0){
"Last Appointment Created: " + $cfFolderResult.Items[0].Subject
"Last Appointment CreatedTime : " + $cfFolderResult.Items[0].DateTimeReceived
}

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

EWS-FAI Module for browsing and updating Exchange Folder Associated Items from PowerShell

Folder Associated Items are hidden Items in Exchange Mailbox folders that are commonly used to hold configuration settings for various Mailbox Clients and services that use Mailboxes. Some common examples of FAI's are Categories,OWA Signatures and WorkHours there is some more detailed documentation in the https://msdn.microsoft.com/en-us/library/cc463899(v=exchg.80).aspx protocol document. In EWS these configuration items can be accessed via the UserConfiguration operation https://msdn.microsoft.com/en-us/library/office/dd899439(v=exchg.150).aspx which will give you access to either the RoamingDictionary, XMLStream or BinaryStream data properties that holds the configuration depending on what type of FAI data is being stored. I've written a number of scripts over the years that target particular FAI's (eg this one that reads the workhours  http://gsexdev.blogspot.com.au/2015/11/finding-timezone-being-used-in-mailbox.html is a good example ) but I didn't have a gene...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.