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 Graph is limited to a m

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You need to authorize it in you tenant (eg build a small ap

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
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.