Wednesday, June 12, 2013

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  

No comments: