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 http://channel9.msdn.com/Events/MEC/2014/EXT301 and  http://channel9.msdn.com/Events/MEC/2014/EXT304 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 "https://outlook.office365.com/ews/odata/Me/Inbox/Messages?`$filter=IsRead eq false" -Credential (get-credential) | foreach-object{$_.value | select Subject}  

Show Calendar Appointments for the next 7 days


  1. Invoke-RestMethod -Uri ("https://outlook.office365.com/ews/odata/Me/Calendar/Events?`$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 "https://outlook.office365.com/ews/odata/Me/Inbox/Messages" -Credential (get-credential) | foreach-object{$_.value}  

There are lots more example of using the new REST commands at http://msdn.microsoft.com/en-us/library/office/dn605892(v=office.15).aspx


2 comments:

J.S said...

Hello,
I've searched everywhere and can't find a way to report on O365 calendars. I have to provide a report of meeting room calendars that has the details such as meeting organizer, attendees, start/end time, if it's recurring, etc.
I need to run this on a scheduled basis for many rooms. MS has stated it could be possible but they aren't sure and I've not found any way to do it after searching for a long time and trying several methods myself.
Can you assist? I have 2 other tasks as well that MS couldn't provide a solid answer on, there is one to be able to run a script and find out if an email has been "read" by one or more users based on the sender or subject as the search criteria, and then how to possibly gather a report on total number of emails sent to domains that are outside the company.

Any help is truly appreciated!
Thank You,
Julie

Glen Scales said...

I would use http://gsexdev.blogspot.com.au/2014/07/reporting-on-expired-recurring-meetings.html as a better based script rather then using the REST Api.

You won't be able to get the attendees from the Meeting rooms copy of the appointment the full attendee list will only ever be available from the organizer copy of the appointment (in office365 you could impersonate the organizer if you have impersonation rights and then get that).

2. You can search a recipient mailbox (probably eDiscovery would be best to find the message and see if it has been read) to see that but wouldn't be very reliable in an OnPrem network you would use ReadTracking but as far as I know that isn't available in o365 http://community.office365.com/en-us/f/148/t/269577.aspx .

3. The Reporting Web Service https://msdn.microsoft.com/en-us/library/office/jj984342.aspx would be where I would start. Short of that you can export the last 7 days of MailTrace logs and then work it from their I think products like promodag http://www.promodag.com/exchange-message-tracking-logs-utility/office-365-message-tracking.aspx you can import and then do fancy report on these logs.