by

EWS Fundamentals : Reporting on the Age of Objects in a Folder

EWS Fundamentals : Reporting on the Age of Objects in a Folder One of many extra frequent questions I get is round a number of the EWS Merchandise Age scripts I’ve written prior to now. If we take the script I launched within the first Fundamentals put up with some straightforward modifications we are able to flip this script that already enumerates all of the Objects in a folder right into a script that can report on age of the Objects in a folder. So on this put up I’ll attempt to clarify a bit higher the adjustments that you’d must make.

EWS Adjustments

The EWS adjustments that have to be made to the fundamentals script are to the Propertyset that’s used within the FindItems Operation. A PropertySet in EWS principally tells Change which properties you wish to be returned. So to make this script as environment friendly as potential we wish to limit Change to simply returning the Dimension and DateTime the messages the place obtained or created. So these are the one adjustments which are wanted for the EWS aspect

$ivItemView =  New-Object Microsoft.Change.WebServices.Information.ItemView(1000) 
$ItemPropset= new-object Microsoft.Change.WebServices.Information.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)
$ItemPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Dimension)
$ItemPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived)
$ItemPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeCreated)
$ivItemView.PropertySet = $ItemPropset

Reporting

When your reporting on the age of Objects your principally aggregating the a part of the Date/Time a Message was obtained. To do the aggregation on this script I am simply utilizing a Hashtable which there’s a very good description of at https://technet.microsoft.com/en-us/library/ee692803.aspx . The best way I am utilizing them to mixture the variety of Messages and the scale of the messages for the 12 months a Message was obtained in. To get the 12 months a message was obtained within the merchandise enumeration part of the script I am utilizing this straightforward operate

$dateVal = $null
if($Merchandise.TryGetProperty([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived,[ref]$dateVal )-eq $false){
$dateVal = $Merchandise.DateTimeCreated
}

What this does is hundreds the DateTimeReceived property into the $dataVal variable and if that property is not accessible (eg the message perhaps a draft) it makes use of the DateTime created property.

As soon as we’ve this worth its only a matter of firstly utilizing the 12 months property of the (DateTime) Class after which we mixture this worth with the Hashtable. Within the Values assortment of the HashTable I am utilizing a Customized object (see https://technet.microsoft.com/en-us/journal/hh750381.aspx) that does the aggregation half.  eg

if($rptCollection.ContainsKey($dateVal.12 months)){
$rptCollection[$dateVal.12 months].TotalNumber += 1
$rptCollection[$dateVal.12 months].TotalSize += [Int64]$Merchandise.Dimension
}
else Choose 12 months,TotalNumber,TotalSize
$rptObj.TotalNumber = 1
$rptObj.12 months = $dateVal.12 months
$rptObj.TotalSize = [Int64]$Merchandise.Dimension
$rptCollection.add($dateVal.12 months,$rptObj)

On the finish of this we are able to simply write the HashTable Values assortment again to output Pipeline utilizing write-object benefiting from the sort-object cmdlet to verify the values are sorted descending by 12 months

Write-Output $rptCollection.Values | Type-Object -Property 12 months -Descending

I’ve put a replica of this script on GitHub right here https://github.com/gscales/Powershell-Scripts/edit/grasp/GetItemAgeFromItemsInFolder.ps1

An instance of working this script would appear to be