Wednesday, March 30, 2016

EWS Basics : Enumerating all the Items in any folder in a Mailbox

I've decided to start a new series on the blog called EWS Basics where I'll post some reusable scripts that are fit for easy customization by those people unfamiliar with EWS. So the basic idea is to have a group of samples where most of the underlying EWS plumbing code is done and you can just plug in whatever customized action you want to do on particular Items

Enumerate all the Items in any folder in a Mailbox

A common task when you want to report on the Item content in a Folder or you want to perform some type of action on Items (Delete,Move,Copy etc) is you will want to enumerate all the items in a folder. One analogy is doing a dir on directory (just don't mention the M: drive!)

So what this sample does is lets you input the MailboxName (PrimarySMTP Address) and Path to the Folder in the mailbox where the Item are  you want to access and it will write back to the pipeline the Items it finds.

So to put that together in a sample say you want to enumerate the Items in the Clutter Folder and output the ReceivedDateTime and Subject you could use the following

Get-FolderItems -MailboxName gscales@domain.com -FolderPath \clutter | Select DateTimeReceived,Subject

If the folder you wanted to access was a subfolder of the Inbox you could use

Get-FolderItems -MailboxName gscales@domain.com -FolderPath \Inbox\Subfolder| Select DateTimeReceived,Subject

Another example of doing something a little more advanced is where you modify the script to produce statistics of the domains of the senders for email in a Mailbox folder. To do this you need to modify the part of the script that enumerates each of the Items so instead of writing the object to the pipeline it does some summarizations. I've added some lines in that uses a HashTable to compile the statistics on Email Domains and then writes out and sorts those values in the last line.

do{    
    $fiItems = $service.FindItems($SubFolderId,$ivItemView)   
 Write-Host ("Processed " + $fiItems.Items.Count)
    #[Void]$service.LoadPropertiesForItems($fiItems,$ItemPropset)  
    foreach($Item in $fiItems.Items){      
  #Process Item
  $EmailAddress = new-object System.Net.Mail.MailAddress($Item.Sender.Address)
  if($rptCollection.ContainsKey($EmailAddress.Host))
  {
   $rptCollection[$EmailAddress.Host].NumberOfItems +=1
   $rptCollection[$EmailAddress.Host].SizeOfItems = $Item.Size
  }
  else
  {
   $rptObj = "" | select Domain,NumberOfItems,SizeOfItems
   $rptObj.Domain = $EmailAddress.Host
   $rptObj.NumberOfItems = 1
   $rptObj.SizeOfItems = $Item.Size
   $rptCollection.Add($EmailAddress.Host,$rptObj)
  }
    }    
    $ivItemView.Offset += $fiItems.Items.Count    
}while($fiItems.MoreAvailable -eq $true) 
Write-Output $rptCollection.Values | Sort-Object -Property NumberOfItems -Descending
}

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


Monday, March 14, 2016

Conversation statistics with EWS

Conversations have been a hot area in the Messaging space recently with many different solutions jockeying for attention such as Yammer, Office365 Groups , Slack, HipChat, Skype for business and many other. Each solution offers a different method to communicate and thread different conversations in different ways  over different clients and communication platforms. Typically in Exchange conversations either take place in Mailboxes or Public folders while Groups are a now a newer offering.

When it comes to reporting, looking at conversations can offer some interesting insights as to when conversations are happening how many people are participating and where a Group or Channel based solution might provide some form of productivity gain or usefulness.

In EWS in Exchange 2010 and greator the findconversation operation allows you to query and group conversation threads in a Mailbox folder and you can then use the information returned about the conversations to get the individual conversation items in a Mailbox using the Batch EWS operations. 

To demonstrate some of this I've come up with a PowerShell module that does just that it first uses the FindCoversation operation to get the Messages from the Inbox of a Mailbox or other folders if you use the FolderPath switch.  The script enumerates all the available message from the conversation that it looks at (eg those with more then 2 participants and 2 messages). It then compiles statistics for each conversation about the

Number of messages, participants
Start and duration of the conversation thread in hours
The original sender of the thread and how many messages they sent
The loud mouth (the person who isn't the originate of the thread  who has responded the most) and number of Messages. from this person

I've put a copy of the module on GitHub here https://github.com/gscales/Powershell-Scripts/blob/master/EWSConversation.ps1 to run the script you pass in the Mailbox you want it to run against and period of days to look back eg

 Get-ConversationStats -MailboxName gscales@datarumble.com -Period 60

If you are moving conversations into a different folder in a Mailbox using a Rule eg from a Distribution list you can report on these folders using the FolderPath parameter eg

 Get-ConversationStats -MailboxName gscales@datarumble.com -Period 60 -FolderPath \Inbox\BlahGroup