Skip to main content

EWS/Powershell Recoverable Items age report for Exchange 2010/13

With Single Item Recovery in Exchange 2010 and Exchange 2013, the Recoverable Items Folder in an Exchange Mailbox becomes one of the things you may want to pay some more special attention to in the course of managing the disk resource that are being consumed within your Exchange Org. The following is a script that does another take on ItemAge reporting by basically crawling every item in a folder and then grouping the DateTimeRecieved (to get the age of the Item) and the ModifiedTime(which is when its deleted). The script produces a report like the following for the Mailbox you run it against

To use the script you just need to enter the SMTPAddress of the Mailbox you want to run it against and as long as you have rights to the mailbox you should get a report.

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

  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4.   
  5. ## Load Managed API dll    
  6. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"    
  7.     
  8. ## Set Exchange Version    
  9. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2    
  10.     
  11. ## Create Exchange Service Object    
  12. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  13.     
  14. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  15.     
  16. #Credentials Option 1 using UPN for the windows Account    
  17. $psCred = Get-Credential    
  18. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  19. $service.Credentials = $creds        
  20.     
  21. #Credentials Option 2    
  22. #service.UseDefaultCredentials = $true    
  23.     
  24. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  25.     
  26. ## Code From http://poshcode.org/624  
  27. ## Create a compilation environment  
  28. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  29. $Compiler=$Provider.CreateCompiler()  
  30. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  31. $Params.GenerateExecutable=$False  
  32. $Params.GenerateInMemory=$True  
  33. $Params.IncludeDebugInformation=$False  
  34. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null  
  35.   
  36. $TASource=@' 
  37.   namespace Local.ToolkitExtensions.Net.CertificatePolicy{ 
  38.     public class TrustAll : System.Net.ICertificatePolicy { 
  39.       public TrustAll() {  
  40.       } 
  41.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  42.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  43.         System.Net.WebRequest req, int problem) { 
  44.         return true; 
  45.       } 
  46.     } 
  47.   } 
  48. '@   
  49. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  50. $TAAssembly=$TAResults.CompiledAssembly  
  51.   
  52. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  53. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  54. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  55.   
  56. ## end code from http://poshcode.org/624  
  57.     
  58. ## 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    
  59.     
  60. #CAS URL Option 1 Autodiscover    
  61. $service.AutodiscoverUrl($MailboxName,{$true})    
  62. "Using CAS Server : " + $Service.url     
  63.      
  64. #CAS URL Option 2 Hardcoded    
  65.     
  66. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  67. #$service.Url = $uri      
  68.     
  69. ## Optional section for Exchange Impersonation    
  70.     
  71. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  72. $PR_MESSAGE_SIZE_EXTENDED = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(3592,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Long);  
  73. $psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  74. $psPropset.Add($PR_MESSAGE_SIZE_EXTENDED)  
  75. # Bind to the MsgFolderRoot folder    
  76. $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::RecoverableItemsDeletions,$MailboxName)     
  77. $RecoverableItemsDeletions = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid,$psPropset)  
  78.   
  79. $rptObject = "" | Select MailboxName,TotalNumberOfItems,TotalSize,AgeLessThan7days,AgeLessThan7daysSize,Age7To30Days,Age7To30DaysSize,Age1to6Months,Age1to6MonthsSize,Age6To12Months,Age6To12MonthsSize,AgeGreator12Months,AgeGreator12MonthsSize,DeletedLessThan7days,DeletedLessThan7daysSize,Deleted7To30Days,Deleted7To30DaysSize,Deleted1to6Months,Deleted1to6MonthsSize,Deleted6To12Months,Deleted6To12MonthsSize,DeletedGreator12Months,DeletedGreator12MonthsSize  
  80. $rptObject.AgeLessThan7daysSize = [INT64]0  
  81. $rptObject.Age7To30DaysSize = [INT64]0  
  82. $rptObject.Age1to6MonthsSize = [INT64]0  
  83. $rptObject.Age6To12MonthsSize = [INT64]0  
  84. $rptObject.AgeGreator12MonthsSize = [INT64]0  
  85. $rptObject.AgeLessThan7days = [INT64]0  
  86. $rptObject.Age7To30Days = [INT64]0  
  87. $rptObject.Age1to6Months = [INT64]0  
  88. $rptObject.Age6To12Months = [INT64]0  
  89. $rptObject.AgeGreator12Months = [INT64]0  
  90. $rptObject.DeletedLessThan7daysSize = [INT64]0  
  91. $rptObject.Deleted7To30DaysSize = [INT64]0  
  92. $rptObject.Deleted1to6MonthsSize = [INT64]0  
  93. $rptObject.Deleted6To12MonthsSize = [INT64]0  
  94. $rptObject.DeletedGreator12MonthsSize = [INT64]0  
  95. $rptObject.DeletedLessThan7days = [INT64]0  
  96. $rptObject.Deleted7To30Days = [INT64]0  
  97. $rptObject.Deleted1to6Months = [INT64]0  
  98. $rptObject.Deleted6To12Months = [INT64]0  
  99. $rptObject.DeletedGreator12Months = [INT64]0  
  100. $rptObject.MailboxName = $MailboxName  
  101. $rptObject.TotalNumberOfItems = $RecoverableItemsDeletions.TotalCount  
  102. $folderSizeVal = $null;  
  103. if($RecoverableItemsDeletions.TryGetProperty($PR_MESSAGE_SIZE_EXTENDED,[ref]$folderSizeVal)){  
  104.     $rptObject.TotalSize = [Math]::Round([Int64]$folderSizeVal / 1mb,2)  
  105. }  
  106. #Define ItemView to retrive just 1000 Items      
  107. $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)  
  108. $itemPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)   
  109. $itemPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived)  
  110. $itemPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeCreated)  
  111. $itemPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::LastModifiedTime)  
  112. $itemPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Size)  
  113. $fiItems = $null      
  114. $itemCnt = 0  
  115. do{   
  116.     $fiItems = $service.FindItems($RecoverableItemsDeletions.Id,$ivItemView)      
  117.     #[Void]$service.LoadPropertiesForItems($fiItems,$psPropset)    
  118.     foreach($Item in $fiItems.Items){      
  119.         $Notadded = $true  
  120.         $dateVal = $null  
  121.         if($Item.TryGetProperty([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived,[ref]$dateVal )-eq $false){  
  122.             $dateVal = $Item.DateTimeCreated  
  123.         }  
  124.         $modDateVal = $null  
  125.         if($Item.TryGetProperty([Microsoft.Exchange.WebServices.Data.ItemSchema]::LastModifiedTime,[ref]$modDateVal)){  
  126.             if($modDateVal -gt (Get-Date).AddDays(-7))  
  127.             {  
  128.                 $rptObject.DeletedLessThan7days++  
  129.                 $rptObject.DeletedLessThan7daysSize += $Item.Size  
  130.                 $Notadded = $false  
  131.             }  
  132.             if($modDateVal -le (Get-Date).AddDays(-7) -band $modDateVal -gt (Get-Date).AddDays(-30))  
  133.             {  
  134.                 $rptObject.Deleted7To30Days++  
  135.                 $rptObject.Deleted7To30DaysSize += $Item.Size  
  136.                 $Notadded = $false  
  137.             }  
  138.             if($modDateVal -le (Get-Date).AddDays(-30) -band $modDateVal -gt (Get-Date).AddMonths(-6))  
  139.             {  
  140.                 $rptObject.Deleted1to6Months++  
  141.                 $rptObject.Deleted1to6MonthsSize += $Item.Size  
  142.                 $Notadded = $false  
  143.             }  
  144.             if($modDateVal -le (Get-Date).AddMonths(-6) -band $modDateVal -gt (Get-Date).AddMonths(-12))  
  145.             {  
  146.                 $rptObject.Deleted6To12Months++  
  147.                 $rptObject.Deleted6To12MonthsSize += $Item.Size   
  148.                 $Notadded = $false  
  149.             }  
  150.             if($modDateVal -le (Get-Date).AddYears(-1))  
  151.             {  
  152.                 $rptObject.DeletedGreator12Months++  
  153.                 $rptObject.DeletedGreator12MonthsSize += $Item.Size   
  154.                 $Notadded = $false  
  155.             }   
  156.         }         
  157.         if($dateVal -gt (Get-Date).AddDays(-7))  
  158.         {  
  159.             $rptObject.AgeLessThan7days++  
  160.             $rptObject.AgeLessThan7daysSize += $Item.Size   
  161.             $Notadded = $false  
  162.         }  
  163.         if($dateVal -le (Get-Date).AddDays(-7) -band $dateVal -gt (Get-Date).AddDays(-30))  
  164.         {  
  165.             $rptObject.Age7To30Days++  
  166.             $rptObject.Age7To30DaysSize += $Item.Size   
  167.             $Notadded = $false  
  168.         }  
  169.         if($dateVal -le (Get-Date).AddDays(-30) -band $dateVal -gt (Get-Date).AddMonths(-6))  
  170.         {  
  171.             $rptObject.Age1to6Months++            
  172.             $rptObject.Age1to6MonthsSize += $Item.Size   
  173.             $Notadded = $false  
  174.         }  
  175.         if($dateVal -le (Get-Date).AddMonths(-6) -band $dateVal -gt (Get-Date).AddMonths(-12))  
  176.         {  
  177.             $rptObject.Age6To12Months++  
  178.             $rptObject.Age6To12MonthsSize += $Item.Size   
  179.             $Notadded = $false  
  180.         }  
  181.         if($dateVal -le (Get-Date).AddYears(-1))  
  182.         {  
  183.             $rptObject.AgeGreator12Months++  
  184.             $rptObject.AgeGreator12MonthsSize += $Item.Size   
  185.             $Notadded = $false  
  186.         }   
  187.     }      
  188.     $ivItemView.Offset += $fiItems.Items.Count      
  189. }while($fiItems.MoreAvailable -eq $true)   
  190. if($rptObject.AgeLessThan7daysSize -gt 0){  
  191.     $rptObject.AgeLessThan7daysSize = [Math]::Round($rptObject.AgeLessThan7daysSize/ 1mb,2)  
  192. }  
  193. if($rptObject.Age7To30DaysSize -gt 0){  
  194.     $rptObject.Age7To30DaysSize = [Math]::Round($rptObject.Age7To30DaysSize/ 1mb,2)  
  195. }  
  196. if($rptObject.Age1to6MonthsSize -gt 0){  
  197.     $rptObject.Age1to6MonthsSize = [Math]::Round($rptObject.Age1to6MonthsSize/ 1mb,2)  
  198. }  
  199. if($rptObject.Age6To12MonthsSize -gt 0){  
  200.     $rptObject.Age6To12MonthsSize = [Math]::Round($rptObject.Age6To12MonthsSize/ 1mb,2)  
  201. }  
  202. if($rptObject.AgeGreator12MonthsSize -gt 0){  
  203.     $rptObject.AgeGreator12MonthsSize = [Math]::Round($rptObject.AgeGreator12MonthsSize/ 1mb,2)  
  204. }  
  205. if($rptObject.DeletedLessThan7daysSize -gt 0){  
  206.     $rptObject.DeletedLessThan7daysSize = [Math]::Round($rptObject.DeletedLessThan7daysSize/ 1mb,2)  
  207. }  
  208. if($rptObject.Deleted7To30DaysSize -gt 0){  
  209.     $rptObject.Deleted7To30DaysSize = [Math]::Round($rptObject.Deleted7To30DaysSize/ 1mb,2)  
  210. }  
  211. if($rptObject.Deleted1to6MonthsSize -gt 0){  
  212.     $rptObject.Deleted1to6MonthsSize = [Math]::Round($rptObject.Deleted1to6MonthsSize/ 1mb,2)  
  213. }  
  214. if($rptObject.Deleted6To12MonthsSize -gt 0){  
  215.     $rptObject.Deleted6To12MonthsSize = [Math]::Round($rptObject.Deleted6To12MonthsSize/ 1mb,2)  
  216. }  
  217. if($rptObject.DeletedGreator12MonthsSize -gt 0){  
  218.     $rptObject.DeletedGreator12MonthsSize = [Math]::Round($rptObject.DeletedGreator12MonthsSize/ 1mb,2)  
  219. }  
  220. $a = "<style>"  
  221. $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"  
  222. $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"  
  223. $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"  
  224. $a = $a + "</style>"  
  225. $rptObject | ConvertTo-Html -As LIST -Head $a | Out-File c:\temp\RetainedItemReport.htm  

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 Gr...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.

Export calendar Items to a CSV file using Microsoft Graph and Powershell

For the last couple of years the most constantly popular post by number of views on this blog has been  Export calendar Items to a CSV file using EWS and Powershell closely followed by the contact exports scripts. It goes to show this is just a perennial issue that exists around Mail servers, I think the first VBS script I wrote to do this type of thing was late 90's against Exchange 5.5 using cdo 1.2. Now it's 2020 and if your running Office365 you should really be using the Microsoft Graph API to do this. So what I've done is create a PowerShell Module (and I made it a one file script for those that are more comfortable with that format) that's a port of the EWS script above that is so popular. This script uses the ADAL library for Modern Authentication (which if you grab the library from the PowerShell gallery will come down with the module). Most EWS properties map one to one with the Graph and the Graph actually provides better information on recurrences then...
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.