Skip to main content

eDiscovery script for reporting on large items in a Mailbox

When it comes to searching a Mailbox with EWS with a Script, eDiscovery on Exchange 2013 makes things a lot easier and faster by basically allowing a Mailbox (and Archive) wide search rather then a folder by folder crawl which you had to do with AQS on Exchange 2010.

I posted a paging sample a few months back to show how you can page through the results of a eDiscovery using the EWS Managed API. The following script is an application of this to produce a report of Items that are larger then a certain size in the Mailbox and Archive. To give the Folder path of the folder where the items are located the script grabs the FolderPaths and produces a report like


To run a discovery just feed it the Mailbox you want to run it against, and to set the size of the Items you want to find you can modify the following variable that holds the KQL query (the following finds items larger then 10 MB)

$KQL = "size>10485760" 

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

  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4.   
  5. $KQL = "size>10485760";            
  6.   
  7. $SearchableMailboxString = $MailboxName;  
  8.   
  9. ## Load Managed API dll    
  10. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"    
  11.     
  12. ## Set Exchange Version    
  13. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013    
  14.     
  15. ## Create Exchange Service Object    
  16. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  17.     
  18. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  19.     
  20. #Credentials Option 1 using UPN for the windows Account    
  21. $psCred = Get-Credential    
  22. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  23. $service.Credentials = $creds        
  24.     
  25. #Credentials Option 2    
  26. #service.UseDefaultCredentials = $true    
  27.     
  28. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  29.     
  30. ## Code From http://poshcode.org/624  
  31. ## Create a compilation environment  
  32. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  33. $Compiler=$Provider.CreateCompiler()  
  34. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  35. $Params.GenerateExecutable=$False  
  36. $Params.GenerateInMemory=$True  
  37. $Params.IncludeDebugInformation=$False  
  38. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null  
  39.   
  40. $TASource=@' 
  41.   namespace Local.ToolkitExtensions.Net.CertificatePolicy{ 
  42.     public class TrustAll : System.Net.ICertificatePolicy { 
  43.       public TrustAll() {  
  44.       } 
  45.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  46.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  47.         System.Net.WebRequest req, int problem) { 
  48.         return true; 
  49.       } 
  50.     } 
  51.   } 
  52. '@   
  53. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  54. $TAAssembly=$TAResults.CompiledAssembly  
  55.   
  56. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  57. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  58. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  59.   
  60. ## end code from http://poshcode.org/624  
  61.     
  62. ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use    
  63.     
  64. #CAS URL Option 1 Autodiscover    
  65. $service.AutodiscoverUrl($MailboxName,{$true})    
  66. "Using CAS Server : " + $Service.url     
  67.      
  68. #CAS URL Option 2 Hardcoded    
  69.     
  70. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  71. #$service.Url = $uri      
  72.     
  73. ## Optional section for Exchange Impersonation    
  74.     
  75. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  76.   
  77. ##get folder Paths  
  78. #Define Function to convert String to FolderPath    
  79. function ConvertToString($ipInputString){    
  80.     $Val1Text = ""    
  81.     for ($clInt=0;$clInt -lt $ipInputString.length;$clInt++){    
  82.             $Val1Text = $Val1Text + [Convert]::ToString([Convert]::ToChar([Convert]::ToInt32($ipInputString.Substring($clInt,2),16)))    
  83.             $clInt++    
  84.     }    
  85.     return $Val1Text    
  86. }   
  87.   
  88.   
  89. function GetFolderPaths{  
  90.     param (  
  91.             $rootFolderId = "$( throw 'rootFolderId is a mandatory Parameter' )",  
  92.             $Archive = "$( throw 'Archive is a mandatory Parameter' )"  
  93.           )  
  94.     process{  
  95.     #Define Extended properties    
  96.     $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);    
  97.     $folderidcnt = $rootFolderId  
  98.     #Define the FolderView used for Export should not be any larger then 1000 folders due to throttling    
  99.     $fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)    
  100.     #Deep Transval will ensure all folders in the search path are returned    
  101.     $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;    
  102.     $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  103.     $PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);    
  104.     #Add Properties to the  Property Set    
  105.     $psPropertySet.Add($PR_Folder_Path);    
  106.     $fvFolderView.PropertySet = $psPropertySet;    
  107.     #The Search filter will exclude any Search Folders    
  108.     $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")    
  109.     $fiResult = $null    
  110.     #The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox    
  111.     do {    
  112.         $fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)    
  113.         foreach($ffFolder in $fiResult.Folders){    
  114.             $foldpathval = $null    
  115.             #Try to get the FolderPath Value and then covert it to a usable String     
  116.             if ($ffFolder.TryGetProperty($PR_Folder_Path,[ref] $foldpathval))    
  117.             {    
  118.                 $binarry = [Text.Encoding]::UTF8.GetBytes($foldpathval)    
  119.                 $hexArr = $binarry | ForEach-Object { $_.ToString("X2") }    
  120.                 $hexString = $hexArr -join ''    
  121.                 $hexString = $hexString.Replace("FEFF""5C00")    
  122.                 $fpath = ConvertToString($hexString)    
  123.             }    
  124.             "FolderPath : " + $fpath    
  125.             if($Archive){  
  126.                 $Script:FolderCache.Add($ffFolder.Id.UniqueId,"\Archive-Mailbox\" + $fpath); 
  127.             } 
  128.             else{ 
  129.                 $Script:FolderCache.Add($ffFolder.Id.UniqueId,$fpath); 
  130.             } 
  131.         }  
  132.         $fvFolderView.Offset += $fiResult.Folders.Count 
  133.     }while($fiResult.MoreAvailable -eq $true)   
  134.     } 
  135. } 
  136.  
  137. $Script:FolderCache = New-Object system.collections.hashtable 
  138. GetFolderPaths -rootFolderId (new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$MailboxName)) -Archive $false   
  139. GetFolderPaths -rootFolderId (new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::ArchiveMsgFolderRoot,$MailboxName)) -Archive $true  
  140.  
  141. $gsMBResponse = $service.GetSearchableMailboxes($SearchableMailboxString, $false); 
  142. $gsMBResponse 
  143. $msbScope = New-Object  Microsoft.Exchange.WebServices.Data.MailboxSearchScope[] $gsMBResponse.SearchableMailboxes.Length 
  144. $mbCount = 0; 
  145. foreach ($sbMailbox in $gsMBResponse.SearchableMailboxes) 
  146. { 
  147.     $msbScope[$mbCount] = New-Object Microsoft.Exchange.WebServices.Data.MailboxSearchScope($sbMailbox.ReferenceId, [Microsoft.Exchange.WebServices.Data.MailboxSearchLocation]::All); 
  148.     $mbCount++; 
  149. } 
  150. $smSearchMailbox = New-Object Microsoft.Exchange.WebServices.Data.SearchMailboxesParameters 
  151. $mbq =  New-Object Microsoft.Exchange.WebServices.Data.MailboxQuery($KQL, $msbScope); 
  152. $mbqa = New-Object Microsoft.Exchange.WebServices.Data.MailboxQuery[] 1 
  153. $mbqa[0] = $mbq 
  154. $smSearchMailbox.SearchQueries = $mbqa; 
  155. $smSearchMailbox.PageSize = 100; 
  156. $smSearchMailbox.PageDirection = [Microsoft.Exchange.WebServices.Data.SearchPageDirection]::Next; 
  157. $smSearchMailbox.PerformDeduplication = $false;            
  158. $smSearchMailbox.ResultType = [Microsoft.Exchange.WebServices.Data.SearchResultType]::PreviewOnly; 
  159. $srCol = $service.SearchMailboxes($smSearchMailbox); 
  160. $rptCollection = @() 
  161.  
  162. if ($srCol[0].Result -eq [Microsoft.Exchange.WebServices.Data.ServiceResult]::Success) 
  163. { 
  164.     Write-Host ("Items Found " + $srCol[0].SearchResult.ItemCount) 
  165.     if ($srCol[0].SearchResult.ItemCount -gt 0) 
  166.     {                   
  167.         do 
  168.         { 
  169.             $smSearchMailbox.PageItemReference = $srCol[0].SearchResult.PreviewItems[$srCol[0].SearchResult.PreviewItems.Length - 1].SortValue; 
  170.             foreach ($PvItem in $srCol[0].SearchResult.PreviewItems) { 
  171.                 $rptObj = "" | select FolderPath,DateTimeReceived,Subject,Size 
  172.                 if($Script:FolderCache.ContainsKey($PvItem.ParentId.UniqueId)){ 
  173.                     $rptObj.FolderPath = $Script:FolderCache[$PvItem.ParentId.UniqueId] 
  174.                     $rptObj.DateTimeReceived = $PvItem.ReceivedTime 
  175.                     $rptObj.Subject = $PvItem.Subject 
  176.                     $rptObj.Size = $PvItem.Size 
  177.                 }else{ 
  178.                     $rptObj.DateTimeReceived = $PvItem.ReceivedTime 
  179.                     $rptObj.Subject = $PvItem.Subject 
  180.                     $rptObj.Size = $PvItem.Size 
  181.                 } 
  182.                 $rptObj 
  183.                 $rptCollection+=$rptObj 
  184.             }                         
  185.             $srCol = $service.SearchMailboxes($smSearchMailbox); 
  186.             Write-Host("Items Remaining : " + $srCol[0].SearchResult.ItemCount);  
  187.         } while ($srCol[0].SearchResult.ItemCount-gt 0 );  
  188.           
  189.     }  
  190.       
  191. }  
  192. $rptCollection | Export-Csv -NoTypeInformation -Path c:\temp\LaItemReport.csv  



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.