Wednesday, July 20, 2011

Using Autodiscover in powershell with the EWS Managed API and Exchange 2010 and hosted Orgs

When it comes to scripting Exchange autodiscover can be a great source for all types of configuration information about an Exchange mailbox and in 2010 it now returns a lot more information about a users mailbox. The EWS Managed API 1.1 provides some new objects that allows a more granular use of Autodiscover which can be helpful in different situations for example during migrations where DNS records for autodiscover may not exist or point to different server etc.

To look at one example lets take Office365 if you want to connect to mailboxes in the Exchange Online cloud using EWS but DNS isn't setup your hosted domain for autodiscover you could use the following script to discover the EWS URL to use.

$mailbox = "glen@mydomain.inthecloudblah.com"
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true }
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$adAutoDiscoverService = New-Object Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010);
$uri=[system.URI] "https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc"
$adAutoDiscoverService.Url = $uri
$adAutoDiscoverService.Credentials = New-Object System.Net.NetworkCredential("username","password")
$adAutoDiscoverService.EnableScpLookup = $false;
$adAutoDiscoverService.RedirectionUrlValidationCallback = {$true};
$adAutoDiscoverService.PreAuthenticate = $true;
$UserSettings = new-object Microsoft.Exchange.WebServices.Autodiscover.UserSettingName[] 1
$UserSettings[0] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::ExternalEwsUrl
$adResponse = $adAutoDiscoverService.GetUserSettings($mailbox , $UserSettings);
$adResponse.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::ExternalEwsUrl]

A few things to note about this script is first the hardcoded URL to autodiscover this means no autodiscover DNS records are needed for the email your looking up.

$uri=[system.URI] "https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc"
$adAutoDiscoverService.Url = $uri

But as I've said the Autodiscover service isn't just not limited to retrieving URL's have a look at the usersettingname enumeration http://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.autodiscover.usersettingname%28v=exchg.80%29.aspx . With this you can do things such as use Alternate mailboxes to see if an online archive is configured.

$UserSettings[0] = [Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::AlternateMailboxes
$adResponse = $adAutoDiscoverService.GetUserSettings($mailbox , $UserSettings);
$adResponse.Settings[[Microsoft.Exchange.WebServices.Autodiscover.UserSettingName]::AlternateMailboxes]


$adAutoDiscoverService.EnableScpLookup = $false;

Which means that the Managed API won't try an Active Directory look up of SCP records for autodiscover which can be very useful to speed up on the discovery process and help during migrations or other AD config issues.

6 comments:

Anonymous said...

I'm glad I stumbled onto your blog. I'm working with the EWS Managed API for Exchange Online and haven't found a ton of useful examples out there.

I was wondering why you set $adAutoDiscoverService.PreAuthenticate = $true? Is that to skip re-authenticating on redirects?

Glen said...

Have a read of http://msdn.microsoft.com/en-us/library/gg591268%28v=EXCHG.140%29.aspx which describes this more in detail

Cheers
Glen

Anonymous said...

I've already checked out that website, plus the Autodiscover Reference (http://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.autodiscover%28v=EXCHG.80%29.aspx), Working with the EWS Managed API (http://msdn.microsoft.com/en-us/library/dd633696.aspx), and Working with the Autodiscover Service (http://msdn.microsoft.com/en-us/library/dd633699.aspx).

That being said, I was hoping for a bit more info. Any enlightenment about this PreAuthenticate property would be much appreciated.

Glen said...

My suggestion would be to always set this otherwise you might find if you try to uploaded large attachments or message that you will get a The request failed. The underlying connection was closed error . similar to this issue http://support.microsoft.com/kb/908573

Cheers
Glen

Rae said...

Ah, thanks for the link! So basically, PreAuthenticate does something behind the scenes to deal with timeouts or running out of memory which tends to be caused by sending lots of data.

Ondrej Ĺ ebela said...

Hi, this procedure should help me to connect to o365 ews faster? Right now we use to connect something like this:
Add-Type -Path "path to dll"
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2010_SP1
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $user, $pass
$service.AutodiscoverUrl($AutodiscoverUrl, {$true})
And I dont know how to merge this two approaches.