Sunday, April 1, 2012

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

No comments:

Post a Comment