Tuesday, June 25, 2013

EWS Powershell default Mailbox Folder ACE audit script

The default Access Control Entry ACE in an Exchange Mailbox Folder DACL controls the rights that all authenticated users have to a particularity mailbox folder. Generally from a security perspective its something you don't want users to use as they may not understand when then set this ACE they are giving all people access to certain information. Eg Giving the default user reviewer rights to your Inbox means everyone in the Exchange org can now read mailbox in you Inbox which may not provide much privacy to your email. The following script will enumerate the permissions on every folder in a Mailbox and then produce a report of the folders where the default ACE isn't set to none. For most users the only folder that should show up in the report in the calendar folder. This script uses EWS but you could also use the Exchange Management Shell Get-MailboxFolderPermission cmdlet.

To run this script you need to feed is with a CSV file that contains the SMTPAddress of the Mailboxes you want it to run against. I've put a download of this script here the script itself looks like

  1. ## Get the Mailbox to Access from the 1st commandline argument  
  2. $Script:rptCollection = @()  
  3. ## Load Managed API dll    
  4. Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"    
  5.     
  6. ## Set Exchange Version    
  7. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2    
  8.     
  9. ## Create Exchange Service Object    
  10. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)    
  11.     
  12. ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials    
  13.     
  14. #Credentials Option 1 using UPN for the windows Account    
  15. $psCred = Get-Credential    
  16. $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  17. $service.Credentials = $creds        
  18.     
  19. #Credentials Option 2    
  20. #service.UseDefaultCredentials = $true    
  21.     
  22. ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates    
  23.     
  24. ## Code From http://poshcode.org/624  
  25. ## Create a compilation environment  
  26. $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider  
  27. $Compiler=$Provider.CreateCompiler()  
  28. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  29. $Params.GenerateExecutable=$False  
  30. $Params.GenerateInMemory=$True  
  31. $Params.IncludeDebugInformation=$False  
  32. $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null  
  33.   
  34. $TASource=@' 
  35.   namespace Local.ToolkitExtensions.Net.CertificatePolicy{ 
  36.     public class TrustAll : System.Net.ICertificatePolicy { 
  37.       public TrustAll() {  
  38.       } 
  39.       public bool CheckValidationResult(System.Net.ServicePoint sp, 
  40.         System.Security.Cryptography.X509Certificates.X509Certificate cert,  
  41.         System.Net.WebRequest req, int problem) { 
  42.         return true; 
  43.       } 
  44.     } 
  45.   } 
  46. '@   
  47. $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)  
  48. $TAAssembly=$TAResults.CompiledAssembly  
  49.   
  50. ## We now create an instance of the TrustAll and attach it to the ServicePointManager  
  51. $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")  
  52. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  53.   
  54. ## end code from http://poshcode.org/624  
  55.     
  56. ## 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    
  57.     
  58. #CAS URL Option 1 Autodiscover    
  59.   
  60.      
  61. #CAS URL Option 2 Hardcoded    
  62.     
  63. #$uri=[system.URI] "https://casservername/ews/exchange.asmx"    
  64. #$service.Url = $uri      
  65.     
  66. ## Optional section for Exchange Impersonation    
  67.     
  68. #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  69.   
  70. # Bind to the Inbox Folder  
  71. #Define Function to convert String to FolderPath    
  72. function ConvertToString($ipInputString){    
  73.     $Val1Text = ""    
  74.     for ($clInt=0;$clInt -lt $ipInputString.length;$clInt++){    
  75.             $Val1Text = $Val1Text + [Convert]::ToString([Convert]::ToChar([Convert]::ToInt32($ipInputString.Substring($clInt,2),16)))    
  76.             $clInt++    
  77.     }    
  78.     return $Val1Text    
  79. }   
  80.   
  81. function Process-Mailbox{  
  82.     param (  
  83.             $SmtpAddress = "$( throw 'SMTPAddress is a mandatory Parameter' )"  
  84.           )  
  85.     process{  
  86.   
  87.         #Define Extended properties    
  88.         $PR_FOLDER_TYPE = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(13825,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Integer);    
  89.         $folderidcnt = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::MsgFolderRoot,$SmtpAddress)  
  90.         #Define the FolderView used for Export should not be any larger then 1000 folders due to throttling    
  91.         $fvFolderView =  New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)    
  92.         #Deep Transval will ensure all folders in the search path are returned    
  93.         $fvFolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep;    
  94.         $psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  95.         $PR_Folder_Path = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26293, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);    
  96.         #Add Properties to the  Property Set    
  97.         $psPropertySet.Add($PR_Folder_Path);    
  98.         $fvFolderView.PropertySet = $psPropertySet;    
  99.         #The Search filter will exclude any Search Folders    
  100.         $sfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($PR_FOLDER_TYPE,"1")    
  101.         $fiResult = $null  
  102.         #Process the Mailbox Root Folder  
  103.         $psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)    
  104.         $psPropset.Add([Microsoft.Exchange.WebServices.Data.FolderSchema]::Permissions);  
  105.         #The Do loop will handle any paging that is required if there are more the 1000 folders in a mailbox    
  106.         do {    
  107.             $fiResult = $Service.FindFolders($folderidcnt,$sfSearchFilter,$fvFolderView)    
  108.             foreach($ffFolder in $fiResult.Folders){    
  109.                 $foldpathval = $null    
  110.                 #Try to get the FolderPath Value and then covert it to a usable String     
  111.                 if ($ffFolder.TryGetProperty($PR_Folder_Path,[ref] $foldpathval))    
  112.                 {    
  113.                     $binarry = [Text.Encoding]::UTF8.GetBytes($foldpathval)    
  114.                     $hexArr = $binarry | ForEach-Object { $_.ToString("X2") }    
  115.                     $hexString = $hexArr -join ''    
  116.                     $hexString = $hexString.Replace("FEFF""5C00")    
  117.                     $fpath = ConvertToString($hexString)    
  118.                 }    
  119.                 "Processing FolderPath : " + $fpath    
  120.                 $ffFolder.Load($psPropset)  
  121.                 foreach($Permission in  $ffFolder.Permissions){  
  122.                     if($Permission.UserId.StandardUser -eq [Microsoft.Exchange.WebServices.Data.StandardUser]::Default){  
  123.                         if($Permission.DisplayPermissionLevel -ne "None"){  
  124.                             $rptObj = "" | Select Mailbox,FolderPath,FolderType,PermissionLevel  
  125.                             $rptObj.Mailbox = $SmtpAddress  
  126.                             $rptObj.FolderPath = $fpath  
  127.                             $rptObj.FolderType = $ffFolder.FolderClass  
  128.                             $rptObj.PermissionLevel = $Permission.DisplayPermissionLevel  
  129.                             $Script:rptCollection += $rptObj  
  130.                         }   
  131.                     }  
  132.                 }  
  133.   
  134.             }   
  135.             $fvFolderView.Offset += $fiResult.Folders.Count  
  136.         }while($fiResult.MoreAvailable -eq $true)   
  137.     }  
  138. }  
  139. Import-Csv -Path $args[0] | ForEach-Object{  
  140.     if($service.url -eq $null){  
  141.         $service.AutodiscoverUrl($_.SmtpAddress,{$true})   
  142.         "Using CAS Server : " + $Service.url   
  143.     }    
  144.     Process-Mailbox -SmtpAddress $_.SmtpAddress  
  145. }  
  146. $Script:rptCollection  

Friday, June 14, 2013

EWS Findpeople workaround for reading the Offline Address Book in Office365 / Exchange Online

A couple of weeks ago I posted this about using the new FindPeople operation in EWS with Exchange 2013 to enumerate through the Global Address List. As I mentioned one pain point around using this new operation on Office365 is that you need to know the AddressList Id which there is no way of dynamically getting via EWS. This has been bugging me for a while so I started thinking about some ways of working around this and one method I found that did work is you can obtain the Id for the Offline Address Book and then query this (which is mostly as good as querying the Online GAL).

To Get the Id of the Offline Address book what you first need to do is use AutoDiscover to get the External OAB url, Then use a normal Get request on this url for the oab.xml file. Then you can parse from the OAB.xml file the Guid value of the OAB which you can transform into an AddressList id that you can then use with EWS to query the OAB. The following C# sample use the EWS Managed API for Autodiscover and then use some WSDL Proxy code to execute the FindPeople Operation

  1.     NetworkCredential ncCred = new NetworkCredential("user@domain.onmicrosoft.com""psword");  
  2.     String mbMailbox = "user@domain.onmicrosoft.com";  
  3.     AutodiscoverService adService = new AutodiscoverService(ExchangeVersion.Exchange2013);  
  4.     adService.Credentials = ncCred;  
  5.     adService.RedirectionUrlValidationCallback = adAutoDiscoCallBack;  
  6.     GetUserSettingsResponse adResponse = adService.GetUserSettings(mbMailbox, (new UserSettingName[2] { UserSettingName.ExternalOABUrl,UserSettingName.ExternalEwsUrl }));  
  7.     String exOABURL = (String)adResponse.Settings[UserSettingName.ExternalOABUrl];  
  8.     String ewsURL = (String)adResponse.Settings[UserSettingName.ExternalEwsUrl];  
  9.     String auDisXML = "";  
  10.     System.Net.HttpWebRequest oabRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create((exOABURL + "oab.xml"));  
  11.   
  12.      
  13.     byte[] bytes = Encoding.UTF8.GetBytes(auDisXML);  
  14.     oabRequest.ContentLength = bytes.Length;  
  15.     oabRequest.ContentType = "text/xml";  
  16.     oabRequest.Headers.Add("Translate""F");  
  17.     oabRequest.Method = "GET";  
  18.     oabRequest.Credentials = ncCred;  
  19.     oabRequest.AllowAutoRedirect = false;  
  20.     WebResponse oabResponse = oabRequest.GetResponse();  
  21.   
  22.     Stream rsResponseStream = oabResponse.GetResponseStream();  
  23.     XmlDocument reResponseDoc = new XmlDocument();  
  24.     reResponseDoc.Load(rsResponseStream);  
  25.     XmlNodeList oabDetails = reResponseDoc.GetElementsByTagName("OAL");  
  26.     String OabGuid = oabDetails[0].Attributes["dn"].Value.Substring(6);  
  27.     OabGuid =  OabGuid.Substring(6, 2) + OabGuid.Substring(4, 2) + OabGuid.Substring(2, 2) + OabGuid.Substring(0, 2) + "-" + OabGuid.Substring(10, 2) + OabGuid.Substring(8, 2) + "-" + OabGuid.Substring(14, 2) + OabGuid.Substring(12, 2) + "-" + OabGuid.Substring(16, 4) + "-" + OabGuid.Substring(20, 12);  
  28.   
  29.   
  30.     ExchangeServiceBinding esb = new ExchangeServiceBinding();  
  31.     esb.Url = ewsURL;  
  32.     esb.Credentials = ncCred;  
  33.     esb.RequestServerVersionValue = new EWSProxy.RequestServerVersion();  
  34.     esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2013;  
  35.   
  36.     FindPeopleType fpType = new FindPeopleType();  
  37.     IndexedPageViewType indexPageView = new IndexedPageViewType();  
  38.     indexPageView.BasePoint = IndexBasePointType.Beginning;  
  39.     indexPageView.Offset = 0;  
  40.     indexPageView.MaxEntriesReturned = 100;  
  41.     indexPageView.MaxEntriesReturnedSpecified = true;  
  42.     fpType.IndexedPageItemView = indexPageView;  
  43.   
  44.   
  45.     fpType.ParentFolderId = new TargetFolderIdType();  
  46.     DistinguishedFolderIdType contactsFolder = new DistinguishedFolderIdType();  
  47.     AddressListIdType adList = new AddressListIdType();  
  48.     adList.Id = OabGuid;  
  49.   
  50.     fpType.ParentFolderId.Item = adList;  
  51.     FindPeopleResponseMessageType fpm = null;  
  52.     do  
  53.     {  
  54.         fpm = esb.FindPeople(fpType);  
  55.         if (fpm.ResponseClass == ResponseClassType.Success)  
  56.         {  
  57.             foreach (PersonaType PsCnt in fpm.People)  
  58.             {  
  59.                 if (PsCnt.EmailAddress.MailboxTypeSpecified) {  
  60.                     Console.WriteLine(PsCnt.EmailAddress.MailboxType);  
  61.                 }  
  62.                 Console.WriteLine( PsCnt.EmailAddress.EmailAddress);  
  63.             }  
  64.             indexPageView.Offset += fpm.People.Length;  
  65.         }  
  66.         else  
  67.         {  
  68.             throw new Exception("Error");  
  69.         }  
  70.     } while (fpm.TotalNumberOfPeopleInView > indexPageView.Offset);    
  71.   
  72.   
  73. }  
  74. internal static bool adAutoDiscoCallBack(string url)  
  75. {  
  76.     return true;  
  77. }  


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