Saturday, February 13, 2010

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"
$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


$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);
$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

1 comment:

Anonymous said...

Hi Glen. Great script.
Your $sidbind line seems to be incorrect though. Shouldn't it be something like:
$sidbind = "LDAP://"

$aceuser = [ADSI]$sidbind