Monday, May 26, 2014

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  



Thursday, May 08, 2014

GPX Route Mail App for Exchange 2013 and Exchange Online

As part of my MEC talk last month I presented a Mail App that would show a Map with route layer added based on the content of a GPX attachment on a received message. For example here's a screenshot of the Application that was activated from a GPX file that was exported from the Google Tracks application .


Mail Apps are a new feature of Exchange2013/Exchange Online that gives you a standard way of extending the functionality of Outlook,OWA and OWA for devices. The way this particular Mail app works is first, in the Manifest file there is a filter so it will only show on messages you open/view that have an attachment eg

  1. <Rule xsi:type="RuleCollection" Mode="And">  
  2.     <Rule xsi:type="ItemIs" FormType = "Read" ItemType="Message" />  
  3.     <Rule xsi:type="ItemHasAttachment" />  
  4. </Rule>  

Next if somebody activates the application the next thing it does is uses the new Attachments API that was added in 2013 SP1 to first get the details of the attachments on the message, it then checks if one of them is a GPX attachment. If there is a GPX attachment it then uses an externally hosted controller to make the GetAttachment Call to EWS using a token obtained for authentication to get the Attachment content which is then returned to the MailApp (this is done using a modified version of the following sample). Once the Mail App has the GPX content from the attachment it then uses leaflet which is a open source javascript library that makes mapping easier and also a GPX plugin that was developed for leaflet to add a GPX layer to the map.

Note in this sample I'm using  openstreetmap if you going to use this in your own environment or develop a similar solution using this code you need to be aware of the tile usage policy http://wiki.openstreetmap.org/wiki/Tile_usage_policy of this or any mapping service you decide to use.

To use a Mail App you need to host all the associated the files yourself somewhere see http://msdn.microsoft.com/en-us/library/office/fp161135(v=office.15).aspx for more details. With this particular application you need to host both the Mail App and also the controller service to allow you to get the attachment content.  I've put a download of the hosted code for the Mail App here https://www.dropbox.com/s/6fzzc3hpli8x5sf/HostedFiles.zip the attachment service uses a modified version of http://code.msdn.microsoft.com/office/Mail-apps-for-Office-Get-38babdc9 I've included a copy of the modified controller in the download.