Using powershell to utilize the Exchange Web Services API
I wanted to write a new guide to show you the thought process that I have when I’m exploring .net classes in Powershell.
And something that most of you use is Exchange so I wrote a few lines on how to explore and use the EWS API.
Downloading the EWS managed API
First download and install the EWS managed API from here.
Connecting to Office 365 with EWS
I would suggest doing this simple for you in the future and make yourself a function:
Now, use that shiny new function to connect to EWS
Now the EWS API has created a ExchangeService object for you in the $ExchangeService variable.
This is the variable that you use mainly to bind to stuff such like calendars, inboxes, public folders etc.
If you want to get an inbox or calendar, you need to bind to it. And to do this you would want to use it in conjunction with a ‘Well known name’ so that exchange knows where to look.
Binding is one of the first things you will need to do if you want to fetch or manipulate items in exchange with EWS.
And the bind is usually, if not always to a folder. Since everything in exchange and outlook is based around a folder structure.
Lets fetch my inbox:
Now we have our variable $Inbox with a folder object that is bound to our inbox.
But what can we do with it?
Found something familiar? I would guess that the method ‘MarkAllItemsAsRead’ is the same method that outlook calls in online mode.
Tho i wouldn’t mess around to much with stuff like Delete or move on my own account.
Let’s find out how to fetch some emails in our inbox, the ‘FindItems’ method looks like a nice candidate:
This tells us that we need to supply the FindItems method with one of the combinations above.
If we want to list everything in our inbox, the most likely candidate is row 3:
So we need to find out what ‘Microsoft.Exchange.WebServices.Data.ItemView’ is.
Fetching items from your inbox
With a quick recap:
We found out that we would try the $Inbox.FindItems() method
To fetch as many items as possible, the most viable option seemed to be the Overload that only needed an “ItemView” object in it
We used the class method “New” on the [Microsoft.Exchange.WebServices.Data.ItemView] class to create a variable named $ItemView
Now, if you’re not the inbox-zero type like me, you would problaly want to press ctrl+c depending on how many emails you have.
Let’s see what we can do with the items…
Exploring items and forwarding email
Some interesting stuff here.
We can forward the email, Reply to it, move it, send it, delete it etc.
What if we wanted to forward it?
This tells us that it problably want some kind of prefix, probably ‘fw’ or something right?
‘Microsoft.Exchange.WebServices.Data.EmailAddress toRecipients’ and ‘System.Collections.Generic.IEnumerable[Microsoft.Exchange.WebServices.Data.EmailAddress] toRecipients’:
This tells us that we will either need to supply one email address or a collection of email addresses.
Let’s explore the ‘Microsoft.Exchange.WebServices.Data.MessageBody’:
Looks like you can supply a BodyType here as well, if you want to send the message with HTML or Text formating.
But let’s skip that and just create a new MessageBody with the string ‘CUSTOMFORWARD: '.
If my theory is right the email will arrive as “CUSTOMFORWARD: " right?
And since you problably can and since it's easy: Let's supply the email as a regular string
Look in your inbox and see the result.
Moving an email
As we saw earlier, there was a ‘Move()’ method as well.
This need either a ‘FolderId’ if you want to move it to a self made folder, another mailbox or to public folders.
Or it wants a ‘WellKnownFolderName’ (as we used earlier).
Let’s move it to Drafts:
And now it shows up in the drafts folder.
Exploring other items
In exchange there is notes, to do lists, calendars and so on. All of those are fetchable with about the same method.