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.