Using the EWS Managed API via PowerShell

As part of the rollout of the Exchange 2010 beta, Microsoft has released a beta API library for working with Exchange Web Services which can also be used in conjunction with an Exchange 2007 server. As with any .NET library, this can be invoked from PowerShell. There is an example here of retrieving the number of unread messages in the Inbox, as well as some attributes from the last message received. Unfortunately, the API documentation doesn’t give PowerShell examples, so figuring out how to invoke the calls requires a bit of trial and error. Here is a script I cobbled together which simply sends an email message:

$dllpath = “C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll”
[void][Reflection.Assembly]::LoadFile($dllpath)

$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$uri=[system.URI] “https://YOUR.SERVER.FQDN/ews/exchange.asmx”
$service.Url = $uri

$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
$message.Subject = “This is a test from EWS”
$message.Body = “I sent this from PowerShell via EWS. Bwah ha ha!”
$message.ToRecipients.Add(“recipient@somewhere.com”)
$message.ToRecipients.Add(“another_recipient@somewhere.com”)
$message.Send.Invoke()

The hardest part was figuring out that the Invoke() method was needed to make the Send method work. Now to figure out permissions….

Leave a Reply