by

EWS Fundamentals : Enumerating all of the Gadgets in any folder in a Mailbox

EWS Fundamentals : Enumerating all of the Gadgets in any folder in a Mailbox I’ve determined to start out a brand new sequence on the weblog known as EWS Fundamentals the place I will publish some reusable scripts which are match for simple customization by these folks unfamiliar with EWS. So the fundamental thought is to have a bunch of samples the place many of the underlying EWS plumbing code is finished and you’ll simply plug in no matter custom-made motion you wish to do on explicit Gadgets

Enumerate all of the Gadgets in any folder in a Mailbox

A standard job while you wish to report on the Merchandise content material in a Folder otherwise you wish to carry out some sort of motion on Gadgets (Delete,Transfer,Copy and so forth) is it would be best to enumerate all of the gadgets in a folder. One analogy is doing a dir on listing (simply do not point out the M: drive!)

So what this pattern does is enables you to enter the MailboxName (PrimarySMTP Deal with) and Path to the Folder within the mailbox the place the Merchandise are  you wish to entry and it’ll write again to the pipeline the Gadgets it finds.

So to place that collectively in a pattern say you wish to enumerate the Gadgets within the Litter Folder and output the ReceivedDateTime and Topic you can use the next

Get-FolderItems -MailboxName gscales@area.com -FolderPath litter | Choose DateTimeReceived,Topic

If the folder you wished to entry was a subfolder of the Inbox you can use

Get-FolderItems -MailboxName gscales@area.com -FolderPath InboxSubfolder| Choose DateTimeReceived,Topic

One other instance of doing one thing somewhat extra superior is the place you modify the script to supply statistics of the domains of the senders for e mail in a Mailbox folder. To do that it is advisable to modify the a part of the script that enumerates every of the Gadgets so as an alternative of writing the article to the pipeline it does some summarizations. I’ve added some strains in that makes use of a HashTable to compile the statistics on Electronic mail Domains after which writes out and kinds these values within the final line.

do{    
$fiItems = $service.FindItems($SubFolderId,$ivItemView)
Write-Host ("Processed " + $fiItems.Gadgets.Rely)
#[Void]$service.LoadPropertiesForItems($fiItems,$ItemPropset)
foreach($Merchandise in $fiItems.Gadgets){
#Course of Merchandise
$EmailAddress = new-object System.Web.Mail.MailAddress($Merchandise.Sender.Deal with)
if($rptCollection.ContainsKey($EmailAddress.Host))
{
$rptCollection[$EmailAddress.Host].NumberOfItems +=1
$rptCollection[$EmailAddress.Host].SizeOfItems = $Merchandise.Measurement
}
else
choose Area,NumberOfItems,SizeOfItems
$rptObj.Area = $EmailAddress.Host
$rptObj.NumberOfItems = 1
$rptObj.SizeOfItems = $Merchandise.Measurement
$rptCollection.Add($EmailAddress.Host,$rptObj)

}
$ivItemView.Offset += $fiItems.Gadgets.Rely
}whereas($fiItems.MoreAvailable -eq $true)
Write-Output $rptCollection.Values | Type-Object -Property NumberOfItems -Descending
}

I’ve posted this script on GitHub https://github.com/gscales/Powershell-Scripts/blob/grasp/EnumerateItemsInFolder.ps1