Skip to content

Instantly share code, notes, and snippets.

@karbomusic
Last active September 10, 2020 18:41
Show Gist options
  • Save karbomusic/4a2eced419cbdd46eee35149b3562ec6 to your computer and use it in GitHub Desktop.
Save karbomusic/4a2eced419cbdd46eee35149b3562ec6 to your computer and use it in GitHub Desktop.
How to create a push subscription without any Exchange dependencies such as the Managed API.
# Creates a super simple EWS Push subscription with just an http request and XML
# Does nothing other than this to show it can be done minus the EWS API
$subscribeRequest = "<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:m='http://schemas.microsoft.com/exchange/services/2006/messages'
xmlns:t='http://schemas.microsoft.com/exchange/services/2006/types'
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
<soap:Header>
<t:RequestServerVersion Version='Exchange2010_SP1' />
</soap:Header>
<soap:Body>
<m:Subscribe>
<m:PushSubscriptionRequest>
<t:FolderIds>
<t:DistinguishedFolderId Id='inbox' />
</t:FolderIds>
<t:EventTypes>
<t:EventType>NewMailEvent</t:EventType>
<t:EventType>CopiedEvent</t:EventType>
<t:EventType>MovedEvent</t:EventType>
<t:EventType>DeletedEvent</t:EventType>
</t:EventTypes>
<t:StatusFrequency>5</t:StatusFrequency>
<t:URL>http://bigmono:40005/</t:URL>
</m:PushSubscriptionRequest>
</m:Subscribe>
</soap:Body>
</soap:Envelope>"
# Change to a valid exchange server
$request = [System.Net.WebRequest]::Create("https://ExchangeServer/ews/exchange.asmx");
$request.UserAgent = ""
Write-Host User-Agent = $request.UserAgent
$request.Method = "POST"
$request.ContentLength = $subscribeRequest.Length
$request.ContentType = "text/xml"
$request.Credentials = Get-Credential
$encoder = new-object System.Text.UTF8Encoding
$buffer = $encoder.Getbytes($subscribeRequest)
$dataStream = $request.GetRequestStream()
$dataStream.Write($buffer, 0, $buffer.Length)
$dataStream.Close()
$response = $request.GetResponse()
$response
$responseStream = $response.GetResponseStream()
$reader = New-Object -TypeName System.IO.StreamReader -ArgumentList $responseStream
$responseText = $reader.ReadToEnd()
$responseText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment