Skip to main content

Creating an Out of Office Board using Remote Powershell, Mailtips and the EWS Managed API

Following on from my previous post on using MailTips and one of my other popular posts the FreeBusy board the following script creates an Out of office board that shows in one page peoples out of office status and if the out of office message is turned on it displays the OOF message for that user.Here's a quick picture of what it produces


So how does it work it takes advantage of MailTips within EWS to get the OOF status of a user and what the OOF message is if its set. If you have a large number of users it batches the MailTips request in batches of 100 to ensure the request is successful. To get the list of users to query it takes advantage of using Remote powershell and uses the Get-Mailbox cmdlet.

The following script is setup to work with Office365 but will work OnPremise as well if change around the remote powershell URL.

The following variables are those that need to be configured to run this script

$UserName = "user@domain.com"
$Password = "password"

This is the username and password to use for both the remote powershell connection and it will also be used in the EWS Connection.

$MailboxName = $UserName

This line is setting the Sender email Address for MailTips to the above username if your using UPN's that are different from your SMTP addresses then you will need to change this variable.

$rpRemotePowershell = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -credential $adminCredential  -Authentication Basic -AllowRedirection

If your using this in a OnPrem environment then you need to change the URL and auth to suit your environment currently this is setup to connect to Office365.

I've put a download of this script here the script itself looks like

  1. $UserName = "user@domain.com"  
  2. $Password = "pasdfawrd."  
  3. $mbHash = @{ }  
  4. $MailboxName = $UserName  
  5.   
  6. $batchSize = 100  
  7. $global:oaBoard = ""  
  8.   
  9. $Mailboxes = @()  
  10.   
  11. $cred = New-Object System.Net.NetworkCredential($UserName,$Password)     
  12. $secpassword = ConvertTo-SecureString $Password -AsPlainText -Force  
  13. $adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName,$secpassword     
  14.   
  15. If(Get-PSSession | where-object {$_.ConfigurationName -eq "Microsoft.Exchange"}){  
  16.     write-host "Session Exists"  
  17. }  
  18. else{  
  19.     $rpRemotePowershell = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -credential $adminCredential  -Authentication Basic -AllowRedirection   
  20.     $importresults = Import-PSSession $rpRemotePowershell   
  21. }   
  22.   
  23. get-mailbox -ResultSize unlimited | where-object {$_.HiddenFromAddressListsEnabled -eq $false}  | foreach-object{  
  24.     if ($mbHash.ContainsKey($_.WindowsEmailAddress.ToString()) -eq $false){  
  25.         $mbHash.Add($_.WindowsEmailAddress.ToString(),$_.DisplayName)  
  26.           
  27.     }  
  28.     $emailAddress = $_.WindowsEmailAddress.ToString()  
  29.     $Mailboxes += $emailAddress  
  30. }  
  31.   
  32. $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"  
  33. [void][Reflection.Assembly]::LoadFile($dllpath)  
  34. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)  
  35. $service.TraceEnabled = $false  
  36.   
  37. $service.Credentials = $cred  
  38. $service.autodiscoverurl($MailboxName,{$true})  
  39.   
  40. function GetMailTips{  
  41. $mbMailboxFolderURI = New-Object System.Uri($service.url)  
  42. $wrWebRequest = [System.Net.WebRequest]::Create($mbMailboxFolderURI)  
  43. $wrWebRequest.CookieContainer =  New-Object System.Net.CookieContainer
  44. $wrWebRequest.KeepAlive = $false;  
  45. $wrWebRequest.Headers.Set("Pragma""no-cache");  
  46. $wrWebRequest.Headers.Set("Translate""f");  
  47. $wrWebRequest.Headers.Set("Depth""0");  
  48. $wrWebRequest.ContentType = "text/xml";  
  49. $wrWebRequest.ContentLength = $expRequest.Length;  
  50. $wrWebRequest.Timeout = 60000;  
  51. $wrWebRequest.Method = "POST";  
  52. $wrWebRequest.Credentials = $cred  
  53. $bqByteQuery = [System.Text.Encoding]::ASCII.GetBytes($expRequest);  
  54. $wrWebRequest.ContentLength = $bqByteQuery.Length;  
  55. $rsRequestStream = $wrWebRequest.GetRequestStream();  
  56. $rsRequestStream.Write($bqByteQuery, 0, $bqByteQuery.Length);  
  57. $rsRequestStream.Close();  
  58. $wrWebResponse = $wrWebRequest.GetResponse();  
  59. $rsResponseStream = $wrWebResponse.GetResponseStream()  
  60. $sr = new-object System.IO.StreamReader($rsResponseStream);  
  61. $rdResponseDocument = New-Object System.Xml.XmlDocument  
  62. $rdResponseDocument.LoadXml($sr.ReadToEnd());  
  63. $RecipientNodes = @($rdResponseDocument.getElementsByTagName("t:RecipientAddress"))  
  64. $Datanodes = @($rdResponseDocument.getElementsByTagName("t:OutOfOffice"))  
  65. for($ic=0;$ic -lt $RecipientNodes.length;$ic++){  
  66.     if($Datanodes[$ic].ReplyBody.Message -eq ""){  
  67.           
  68.         $global:oaBoard = $global:oaBoard + "<tr>"  + "`r`n"  
  69.         $global:oaBoard = $global:oaBoard + "<td>" + $mbHash[$RecipientNodes[$ic].EmailAddress] + "</td>"  + "`r`n"  
  70.         $global:oaBoard = $global:oaBoard + "<td bgcolor=`"#41A317`">In the Office</td>"  + "`r`n"  
  71.         $global:oaBoard = $global:oaBoard + "<td></td>"  + "`r`n"  
  72.         $global:oaBoard = $global:oaBoard + "</tr>"  + "`r`n"  
  73.     }  
  74.     else{  
  75.         $global:oaBoard = $global:oaBoard + "<tr>"  + "`r`n"  
  76.         $global:oaBoard = $global:oaBoard + "<td>" + $mbHash[$RecipientNodes[$ic].EmailAddress] + "</td>"  + "`r`n"  
  77.         $global:oaBoard = $global:oaBoard + "<td bgcolor=`"#153E7E`">Out of the Office</td>"  + "`r`n"  
  78.         $global:oaBoard = $global:oaBoard + "<td>" + $Datanodes[$ic].ReplyBody.Message  + "</td>" + "`r`n"  
  79.         $global:oaBoard = $global:oaBoard + "</tr>"  + "`r`n"  
  80.     }  
  81. }  
  82. }  
  83.   
  84.   
  85. $expHeader = @" 
  86. <?xml version="1.0" encoding="utf-8"?> 
  87. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  88. <soap:Header><RequestServerVersion Version="Exchange2010_SP1" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />  
  89. </soap:Header>  
  90. <soap:Body>  
  91. <GetMailTips xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">  
  92. <SendingAs>  
  93. <EmailAddress xmlns="http://schemas.microsoft.com/exchange/services/2006/types">$MailboxName</EmailAddress>  
  94. </SendingAs>  
  95. <Recipients>  
  96. "@  
  97.   
  98. $global:oaBoard = $global:oaBoard + "<table><tr bgcolor=`"#95aedc`">" +"`r`n"  
  99. $global:oaBoard = $global:oaBoard + "<td align=`"center`" style=`"width=200;`" ><b>User</b></td>" +"`r`n"  
  100. $global:oaBoard = $global:oaBoard + "<td align=`"center`" style=`"width=200;`" ><b>Status</b></td>" +"`r`n"  
  101. $global:oaBoard = $global:oaBoard + "<td align=`"center`" style=`"width=200;`" ><b>Message</b></td>" +"`r`n"  
  102. $global:oaBoard = $global:oaBoard + "</tr>" + "`r`n"  
  103.   
  104. $expRequest = $expHeader  
  105.   
  106. $bCount =0  
  107. foreach($mbMailbox in $Mailboxes){  
  108.     $bCount++  
  109.     $expRequest = $expRequest + "<Mailbox xmlns=`"http://schemas.microsoft.com/exchange/services/2006/types`"><EmailAddress>$mbMailbox</EmailAddress></Mailbox>"   
  110.     if($bCount -eq $batchSize){  
  111.         $expRequest = $expRequest + "</Recipients><MailTipsRequested>OutOfOfficeMessage</MailTipsRequested></GetMailTips></soap:Body></soap:Envelope>"  
  112.         GetMailTips  
  113.         $expRequest = $expHeader  
  114.         $bCount = 0  
  115.     }  
  116. }  
  117. if($bCount -ne 0){  
  118.     $expRequest = $expRequest + "</Recipients><MailTipsRequested>OutOfOfficeMessage</MailTipsRequested></GetMailTips></soap:Body></soap:Envelope>"  
  119.     GetMailTips  
  120. }  
  121. $global:oaBoard = $global:oaBoard + "</table>"  + "  "   
  122. $global:oaBoard | out-file "c:\oofboard.htm"  

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You need to authorize it in you tenant (eg build a small ap

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.