Skip to main content

Using EWS to calculate the age of Items and affect of archive and retention policies in Exchange 2010

While there are plenty of cmdlets within the Exchange Management Shell to calculate the Mailbox and Folder sizes and even Item sizes within public folders there is no cmdlets you can currently use to look at the age of the content within a mailbox to see how old it is and what affect an retention policy may have for example you may want to know how much data will be shifted to an archive store(Search-Mailbox kind of does it but...). To look at Mailbox Content the EWS Managed API provides a easy entry point and the flexibility to do this . On Exchange 2010 to scan for content between a particular date range using a Content Index query via Exchange Search is the quickest and most efficient way of querying this data. The EWS Managed API allows you to perform a CI search via Exchange Search using the AQS querystring overload parameter of the FindItems method see http://msdn.microsoft.com/en-us/library/ee693615%28v=exchg.140%29.aspx.

With AQS the operator .. can be used to search between a range of values for example you could use it to search for Messages between 5 and 10 MB using a AQS String like System.Size:1mb..5mb. When searching for age based content you want to search for items between a particular date range for example to search for items 3 years old you could use 01/01/1990..01/01/2008.

What I've done is create a EWS Managed API script that put this all together it firstly does a query to get the folder hierarchy adding a few other useful properties like the folderpath and the current folder size then using measure-object this sums a collection of folder items based on the Item size. The script produces a CSV file of all the folders in a mailbox that contain items with the Daterange and what the size of those items is and what percentage of the total size of the mailbox folder is the output should look like



With this script the following two variables control the data range for the AQS query and the mailboxes to access

$Range = "01/01/1990..01/01/2008" 
$MailboxName = "user@domain.com"

I've put a download of this script here the code looks like


  1. $Range = "01/01/1990..01/01/2008"  
  2. $MailboxName = "user@domain.com"  
  3.   
  4. $AQSString = "System.Message.DateReceived:" + $Range  
  5. $rptCollection = @()  
  6.   
  7.   
  8. function ConvertToString($ipInputString){  
  9.     $Val1Text = ""  
  10.     for ($clInt=0;$clInt -lt $ipInputString.length;$clInt++){  
  11.             $Val1Text = $Val1Text + [Convert]::ToString([Convert]::ToChar([Convert]::ToInt32($ipInputString.Substring($clInt,2),16)))  
  12.             $clInt++  
  13.     }  
  14.     return $Val1Text  
  15. }  
  16.   
  17.   
  18. $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"  
  19. [void][Reflection.Assembly]::LoadFile($dllpath)  
  20. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)  
  21. #$service.Credentials = New-Object System.Net.NetworkCredential("username","password")  
  22.   
  23. $windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()  
  24. $sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"  
  25. $aceuser = [ADSI]$sidbind  
  26. $service.AutodiscoverUrl($MailboxName,{$true})  
  27. $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);  
  28.   
  29. "Checking : " + $MailboxName   
  30. $folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)  
  31. $fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)  
  32. $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;  
  33. $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)  
  34. $PR_MESSAGE_SIZE_EXTENDED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3592,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Long);  
  35. $PR_DELETED_MESSAGE_SIZE_EXTENDED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26267,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Long);  
  36. $PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);  
  37.   
  38. $psPropertySet.Add($PR_MESSAGE_SIZE_EXTENDED);  
  39. $psPropertySet.Add($PR_Folder_Path);  
  40. $fvFolderView.PropertySet = $psPropertySet;  
  41. $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")  
  42. $fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)  
  43. foreach($ffFolder in $fiResult.Folders){  
  44.     "Processing : " + $ffFolder.displayName  
  45.     $TotalItemCount =  $TotalItemCount + $ffFolder.TotalCount;  
  46.     $FolderSize = $null;  
  47.     $FolderSizeValue = 0  
  48.     if ($ffFolder.TryGetProperty($PR_MESSAGE_SIZE_EXTENDED,[ref] $FolderSize))  
  49.     {  
  50.         $FolderSizeValue = [Int64]$FolderSize  
  51.     }  
  52.     $foldpathval = $null  
  53.     if ($ffFolder.TryGetProperty($PR_Folder_Path,[ref] $foldpathval))  
  54.     {  
  55.       
  56.     }  
  57.     $binarry = [Text.Encoding]::UTF8.GetBytes($foldpathval)  
  58.     $hexArr = $binarry | ForEach-Object { $_.ToString("X2") }  
  59.     $hexString = $hexArr -join ''  
  60.     $hexString = $hexString.Replace("FEFF""5C00")  
  61.     $fpath = ConvertToString($hexString)  
  62.     $fiFindItems = $null  
  63.     $ItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)  
  64.     $psPropertySet1 = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)  
  65.     $psPropertySet1.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Size)  
  66.     $ItemView.PropertySet  
  67.     $itemCollection = @()  
  68.     do{  
  69.         $fiFindItems = $ffFolder.findItems($AQSString,$ItemView)  
  70.         $ItemView.offset += $fiFindItems.Items.Count  
  71.         foreach($Item in $fiFindItems.Items){  
  72.             $rptobject = "" | select Size  
  73.             $rptobject.Size = $Item.Size  
  74.             $itemCollection +=$rptobject  
  75.         }  
  76.     }while($fiFindItems.MoreAvailable -eq $true)  
  77.     $outObj =  $itemCollection | Measure-Object Size -Sum -Average -Min -Max  
  78.     if($outObj -ne $null){  
  79.         Add-Member -InputObject $outObj -MemberType NoteProperty -Name Mailbox -Value $MailboxName  
  80.         Add-Member -InputObject $outObj NoteProperty -Name Folder -Value $ffFolder.DisplayName  
  81.         Add-Member -InputObject $outObj NoteProperty -Name FolderPath -Value $fpath  
  82.         Add-Member -InputObject $outObj NoteProperty -Name TotalFolderSize -Value $FolderSizeValue  
  83.         Add-Member -InputObject $outObj NoteProperty -Name DateRange -Value $Range  
  84.         $rptCollection += $outObj  
  85.     }  
  86. }  
  87. $rptCollection | select Mailbox,Folder,FolderPath,@{label="FolderSize(MB)";expression={[math]::Round($_.TotalFolderSize/1MB,2)}},@{label="RangeSize(MB)";expression={[math]::Round($_.Sum/1MB,2)}},@{label="RangeCount";expression={$_.Count}},@{label="PercentOfSize";expression={'{0:P0}' -f ($_.Sum/$_.TotalFolderSize)}} | export-csv c:\temp\MbAgeReport.csv -NoTypeInformation  

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.