Friday, April 25, 2014

Oneliner Mailbox access with Powershell in Exchange Online using the oData preview

One of the things that was announced at MEC recently was Microsoft's Cloud first strategy with Exchange, which basically means that new feature will appear in Exchange Online first and then at some later date make their way into the OnPremise version of Exchange. One of these new cloud first features is the OData API for Mailbox data which is a REST based API for accessing mailbox data (as apposed to EWS which is a SOAP based API for accessing mailbox data). JSON and REST have become the standard for building WebAPI's over the past few years and every man and his dog (Google, Facebook,Twitter,Apple etc) are now using this so its exciting to see Exchange make this move.

If you want to find out more about it and you have a couple of hours to watch some good video's I would check the following two presentations from MEC and which will give you a good grounding in both oData  and also the new consent framework which is important if your building modern apps. 

One of the interesting things your can do with these REST API is using the Invoke-RestMethod cmdlet in Powershell v4 is now get access to Mailbox data with one line of code. Here are a few samples to wet your appetite and get you started (note this API is still in preview so this is subject to change/errors and bugs)

Show Unread Inbox Messages

  1. Invoke-RestMethod -Uri "`$filter=IsRead eq false" -Credential (get-credential) | foreach-object{$_.value | select Subject}  

Show Calendar Appointments for the next 7 days

  1. Invoke-RestMethod -Uri ("`$filter=Start le " + (Get-Date).ToUniversalTime().AddDays(7).ToString("yyyy-MM-ddThh:mm:ssZ") + " and End ge " + (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddThh:mm:ssZ")) -Credential (Get-Credential) | foreach-object{$_.Value}  

Show Contacts

  1. Invoke-RestMethod -Uri "" -Credential (get-credential) | foreach-object{$_.value}  

There are lots more example of using the new REST commands at