Thursday, February 26, 2015

PowerShell + EAS : Getting Started

This is the first post on a series of blog posts concentrated around understanding Exchange ActiveSync Protocol as this is the underlying protocol which Mobile devices use in order to connect to the Exchange Server.

The whole idea is to be able to craft EAS requests and parse the Server responses using PowerShell in order to understand the protocol better.

Hint - Take a look at the cmdlet Invoke-WebRequest, it will be used to craft the Web requests later on.


Why I want to do this ?
Because when you start poking around you learn the Product better :).

This is a getting started post and I am following and porting most of the code already written in C# at MobilityDojo.net to PowerShell.

This post is about giving you a hang of how to make Web Requests to an Exchange Server's EAS endpoint and parse them to get insight in the process.

Now if you setup mail for a User in a Mobile device and use Fiddler as reverse proxy to analyze the communication , you will see the below :



Skip Verification Checks


Add the below line of code to your Script if you don't bother to perform the Server certificate validation, this is only for testing Environments.


001
002
003

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}



Create the Authorization header 




Enter the credentials of a mail enabled User in (Domain\Username or UserPrincipalName format )


001
002
003
004
$Credential = Get-Credential
$EncodedUsernamePassword = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($('{0}:{1}' -f $Credential.UserName, $Credential.GetNetworkCredential().Password)))
$Headers = @{'Authorization' = "Basic $($EncodedUsernamePassword)"}


Hit /Microsoft-Server-ActiveSync




001
002
$URL = 'https://dexexch.dex.com/Microsoft-Server-ActiveSync'
$response = Invoke-WebRequest -Uri $URL -Headers $Headers -Method Options

Now take a look at the headers returned from the Exchange Server 


PS>$response = Invoke-WebRequest -Uri $URL -Headers $Headers -Method Options                                                                
PS>$response.Headers                                                                                                                      
                                                                                                                                            
Key                                                                    Value                                                                
---                                                                    -----                                                                
Allow                                                                  OPTIONS,POST                                                         
MS-Server-ActiveSync                                                   14.2                                                                 
MS-ASProtocolVersions                                                  2.0,2.1,2.5,12.0,12.1,14.0,14.1                                      
MS-ASProtocolCommands                                                  Sync,SendMail,SmartForward,SmartReply,GetAttachment,GetHierarchy,C...
Public                                                                 OPTIONS,POST                                                         
Content-Length                                                         0                                                                    
Cache-Control                                                          private                                                              
Date                                                                   Thu, 26 Feb 2015 14:59:11 GMT                                        
Server                                                                 Microsoft-IIS/7.5                                                    
X-AspNet-Version                                                       2.0.50727                                                            
X-Powered-By                                                           ASP.NET                                                              
                                                                                                                                            


In a typical EAS communication after Autodiscovery takes place this is the first step as the Client determines the EAS commands & version supported by the remote Exchange Server. We will be using this code over when we start working through the EAS protocol.


Resources:

Original post using C# @MobilityDojo.net
http://mobilitydojo.net/2011/08/16/exchange-activesync-building-blockswarming-up/


Working with .NET call backs
http://www.nivot.org/post/2009/07/18/PowerShell20RCWorkingWithNETCallbacksPart1Synchronous

No comments:

Post a Comment