Updating User properties using GraphAPI

Using Autodiscovery in EWS Managed API via Windows Powershell

This is part 3 of the Series from the previous Post where I demonstrated how to setup Powershell Client for EWS Managed API for Office365 Mailboxes?

In the 1st post we discussed how to set EWS client using windows Powershell and how to save and send an email from the mailbox using EWS.

In the in 2nd Post we learned how to use Application Impersonation to work with bulk Mailbox management via EWS.

If you would like to go through the 1st and 2nd part of the series please go to below link.

In this part we will learn how to use Auto Discovery to Query EWS URL for a mailbox which is in Office 365.

Note: Before you can start working with EWS Managed API please make sure you have installed EWS managed API 2.0 on your machine, I have provided the download link in my first post.

if you landed this page directly, I consider you are familiar with the EWS and have the API installed Already if not I would suggest you to first go through the 1st post as there I have explained the best way to connect to a Mailbox via EWS in Office365 via setting up EWS Url Manually.

Auto Discovery is good for On Premise Exchange where EWS end point may change, I do not recommend to use Auto discovery Method for office 365 as end point is always same for all the office 365 tenant hence it would be best to setup the EWS url Manually, but if you want to automate the Client setup Auto discovery provide big help.

So Let’s Create a Powershell client for EWS API, and save the configuration information in an Array called “$Service” 

$EWSdllPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
Import-Module $EWSDllpath
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService

Now Let’s Recall $service Array


We see attribute EnableSCPLookup is set to True and attribute Credentials is Empty so Lets add the Credentials for auth.

$user = "Admin@sunil-chauhan.blogspot.in"
$pass = "Password"
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $user, $pass


if we are using a domain joined machine and trying to autodiscover an on premise Exchange mailbox then this configuration is fine but as we are going to try auto discover Exchange online we need to setup this attribute EnableSCPLookup to false, we would not want to do a SCP lookup Search for an exchange online mailbox.

$Service.EnableSCPLookup = $False
Now we have client setup with basic details so next let’s try auto discovery for an office365 Mailbox by running the below cmd.

$service.AutodiscoverUrl(‘Admin@sunil-chauhan.blogspot.in’)

This is expected to be failed throwing below Exception error.

"Autodiscover blocked a potentially insecure redirection to https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml.
To allow Autodiscover to follow the redirection, use the AutodiscoverUrl(string,AutodiscoverRedirectionUrlValidationCallback) overload."

This happens because auto discover process finds a CNAME redirecting to autodiscover-s.outlook.com, because this might pose a security risk.

the AutoDiscoverUrl method aborts  the discovery process and throws the Exception displayed above.

To handel this situation we can set the AutodiscoverRedirectionUrlValidationCallback to $true.

$service.AutodiscoverUrl(‘Admin@sunil-chauhan.blogspot.in’, {$true})

This is expected to run without any error, If command did not return any error then we have successfully Queried the EWS url.

Let’s recall the $service Array to verify the same.


We see the EWS url is returned by the Auto discover for the mailbox, and now we are good to go inside the mailbox.

This is end of my 3rd post, where we have learned to use autodiscovery to get EWS end point for Client configuration, I hope you like my post, don’t forget to leave your comment feedback and suggestions.

Comments

  1. Thank you so much! This post is perfect!! I've been wrestling with the problem of connecting to a mailbox in O365 with EWS off and on for about two weeks before I finally found this, and it worked perfectly! Also, I appreciate the succinct-ness of your writing style as well.

    ReplyDelete

Post a Comment