Hmm...This Might Work

Solutions from a day long since past
posts - 20, comments - 7, trackbacks - 0

Wednesday, March 18, 2015

Using Exchange Web Services Managed API in PowerShell

I’ve been finding myself in the Exchange 2013 world for the last few months, helping with some administration and updates. As a result I stumbled across an unknown, yet cool (to me)  Exchange API. Naturally I couldn’t resist trying this out in PowerShell. For those who just want working script, here it is. This script will return the most recent 10 items.

# This requires the Exchange Web Services Managed API to be installed on the computer where this script is being ran

# Download at - http://www.microsoft.com/en-us/download/confirmation.aspx?id=42022

Add-Type -Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.1\Microsoft.Exchange.WebServices.dll"

#Connetion - https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.exchangeversion%28v=exchg.80%29.aspx

$EmailAccount = "your email address"

#Change the Exchange Version to work with your environment

$EWS = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1)

#Change the “UseDefaultCredentials” to false if you want to specify alternate creds

#$EWS.UseDefaultCredentials = $false

$EWS.AutodiscoverUrl($EmailAccount)

$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($ews,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox)

$mailitems = $inbox.FindItems(10)

$mailitems | ForEach {$_.Load()}

$mailitems | Select Sender,Subject,Body

For those of you who are still with me…a little history.

The Exchange Web Services (EWS) Managed API appears to have come onto the scene in early 2009 (Where was I?) with it’s Beta 1.0 release. Fast forward to today, it’s in its 2.2 release and is applicable to Exchange 2007 SP1 and UP including Office 365.  This API can be used to work with e-mail messages, calendar, task, and contact information…Translation it’s so much easier for a non-developer (like me!) to harness Exchange resources without dealing with the underlying SOAP interface of EWS. I mean how cool is it that Microsoft took the time to make a wrapper to get to the EWS.

The construction was super simple, and is executed like this.

1.) Create the client object which looks like this

image

2.) Set the URL by passing the “AutodiscoverURL()” method your email address. This will go out to your Exchange environment to get and fill the object with the correct “Exchange.asmx” web service.
This might be something like “https://mail.contoso.com/EWS/Exchange.asmx”

3.) Create a variable for the inbox folder

4.) Create a variable for mailitems filling it with a search from the inbox using the “FindItems()” method. Incidentally this is where you might decide to do other things like use the “MarkAllItemsAsRead()” method to take care of those pesky unread items should you desire.

5.) Loop through each of the mail items so they are loaded into PowerShell using the “Load()” method

6.) Display them selecting whatever fields you deem important. For the sake of this demonstration it “Sender,Subject,Body”

 

All this to say I really am wildly excited about being able to use this as an alternative method to send e-mail from PowerShell Scripts where I use the .net method. Not to mention demonstrating how powerful and friendly powershell really is. Past that I can think of application within SharePoint synchronizing a calendar and task items from a mailbox without a third party program. I’m not saying this is the end all be all by any means but it’s a nice new egg of putty I can make immediate use of to fill some gaps until a better solution can be designed and implemented.

It certainly made me think…”Hmm…This might work”  for this,that, and the other thing too! Smile

posted @ Wednesday, March 18, 2015 5:24 PM | Feedback (0) | Filed Under [ Exchange Powershell SharePoint 2013 ]

Powered by: