Skip to main content

FreeBusy In/Out Board for Exchange 2007 using EWS and Powershell

Update version see here

Firstly this isn't a new idea there's been a similar script on CDO live http://www.cdolive.com/inoutboard.htm for years that uses CDO 1.2. I've had a couple of people ask about doing this in Powershell and EWS using the new availability service on 2007 so here it is.

How does it work

To get the Free/Busy status of another mailbox in Exchange you need to make use of the getuseravailability operation in EWS. To be able to query another users availability you must have been granted rights to the free/busy information for that users Mailbox by default the default ACL gives users the right to see Free/Busy availability but not the newer Subject and Location information made available in 2007. To change the default Free/Busy Permission you can use something like this post A more granular approach would be to assign these rights to a specific user or group eg something like this would add a group to calendar if it didn't exist


[void][Reflection.Assembly]::LoadFile("c:\temp\EWSUtil.dll")
$mbMailboxEmail = "krudd@domain.com"
$GrouptoAdd = "DefaultPermsGroup@domain.com"
$exists = $false
$calutil = new-object EWSUtil.CalendarUtil($mbMailboxEmail,$false, "username","password","domain",$null)
for ($cpint=0;$cpint -lt $calutil.CalendarDACL.Count; $cpint++){
if ($calutil.CalendarDACL[$cpint].UserId.DistinguishedUserSpecified -ne $true){
$sidbind = "LDAP://&ltsid=">"
$AceName = $ace.IdentityReference.Value
$aceuser = [ADSI]$sidbind
if ($aceuser.objectClass[1] -eq "group"){
if ($aceuser.mail -eq $GrouptoAdd){$exists = $true}
}
}
}
if($exists -eq $false){
"Group Doesn't Exist Adding"
$calutil.CalendarDACL.Add($calutil.Reviewer($GrouptoAdd))
$calutil.FreeBusyDACL.Add($calutil.FolderReviewer($GrouptoAdd))
$calutil.Update()
}
else{
"Group Exists Do Nothing"
}



Okay once you have the permissions sorted you can look at actually querying the free/busy information. To do this I've wrapped a few methods that do this using some EWS code in my EWSUtil powershell library so once you've made a connection to the mailbox all you need is to fire the method with firstly an array of mailbox email addresses you want to include in you FB board the duration which consists of the start and end time and the last parameter is the granularity for the Free/Busy time slot eg 60,30 or 15 minutes. So a example of this if you wanted to produce a freeBusy board that create a board for 8:30 to 5:00 with a 30 minute slot period you would need something like this

$drDuration = new-object EWSUtil.EWS.Duration
$drDuration.StartTime = [DateTime]::Parse([DateTime]::Now.ToString("yyyy-MM-dd 08:30"))
$drDuration.EndTime = [DateTime]::Parse([DateTime]::Now.ToString("yyyy-MM-dd 17:30"))
$fbents = $ewc.GetAvailiblity($mbs, $drDuration, 30)

This would produce something that look like this

Actually thats jumping ahead a little we also need some code to generate a String array of Mailbox email addressses to use with the GetAvailiblity method. There are several methods to do this using a custom ADSI query, Group Membership or using Get-Mailbox with a specific filter. The limitaiton here is that you need to limit that array you feed into the method to no more then 100 mailboxes. An example of using Get-Mailbox to build an array would look like

$mbHash = @{ }

get-mailbox | foreach-object{
if ($mbHash.ContainsKey($_.WindowsEmailAddress.ToString()) -eq $false){
$mbHash.Add($_.WindowsEmailAddress.ToString(),$_.DisplayName)
}
}
$mbs = @()
foreach($key in $mbHash.keys){
$mbs += $key
}

The second hashtable I use later to map email address's back to display names.

What the method returns is a nested hashtable for the slots you requested with the Subject and Location information indexed to each of the time slot. I've used the Time and Location information as Mouse over titles in the above board so for example when you mouse over a cetain table cell you will get information about that segment eg


Okay thats it some relatively simple powershell code does the color mapping and exporting to a Html file. I've put a download of the script here. To use this script you need to use the latest EWSUtil Powershell library which you can download from http://msgdev.mvps.org/exdevblog/ewsutil.zip. For more information on using the library and connection and authentication options this is documented in another post. The code itself looks like

[void][Reflection.Assembly]::LoadFile("C:\temp\EWSUtil.dll")

$casUrl = "https://servername/ews/exchange.asmx"
$mbHash = @{ }

get-mailbox | foreach-object{
if ($mbHash.ContainsKey($_.WindowsEmailAddress.ToString()) -eq $false){
$mbHash.Add($_.WindowsEmailAddress.ToString(),$_.DisplayName)
}
}
$mbs = @()
foreach($key in $mbHash.keys){
$mbs += $key
}

$ewc = new-object EWSUtil.EWSConnection($mbMailboxEmail,$false, "usernames", "password", "domain",$casUrl)
$drDuration = new-object EWSUtil.EWS.Duration
$drDuration.StartTime = [DateTime]::Parse([DateTime]::Now.ToString("yyyy-MM-dd 08:30"))
$drDuration.EndTime = [DateTime]::Parse([DateTime]::Now.ToString("yyyy-MM-dd 17:30"))
$fbents = $ewc.GetAvailiblity($mbs, $drDuration, 30)
$frow = $true
foreach($key in $fbents.keys){
if ($frow -eq $true){
$fbBoard = $fbBoard + "<table><tr bgcolor=`"#95aedc`">" +"`r`n"
$fbBoard = $fbBoard + "<td align=`"center`" style=`"width=200;`" ><b>User</b></td>" +"`r`n"
for($stime = $drDuration.StartTime;$stime -lt $drDuration.EndTime;$stime = $stime.AddMinutes(30)){
$fbBoard = $fbBoard + "<td align=`"center`" style=`"width=50;`" ><b>" + $stime.ToString("HH:mm") + "</b></td>" +"`r`n"
}
$fbBoard = $fbBoard + "</tr>" + "`r`n"
$frow = $false
}
for($stime = $drDuration.StartTime;$stime -lt $drDuration.EndTime;$stime = $stime.AddMinutes(30)){
$valuehash = $fbents[$key]
if ($stime -eq $drDuration.StartTime){
$fbBoard = $fbBoard + "<td bgcolor=`"#CFECEC`"><b>" + $mbHash[$valuehash[$stime.ToString("HH:mm")].MailboxEmailAddress.ToString()] + "</b></td>" + "`r`n"
}
switch($valuehash[$stime.ToString("HH:mm")].FBStatus.ToString()){
"0" {$bgColour = "bgcolor=`"#41A317`""}
"1" {$bgColour = "bgcolor=`"#52F3FF`""}
"2" {$bgColour = "bgcolor=`"#153E7E`""}
"3" {$bgColour = "bgcolor=`"#4E387E`""}
"4" {$bgColour = "bgcolor=`"#98AFC7`""}
"N/A" {$bgColour = "bgcolor=`"#98AFC7`""}
}
$title = "title="
if ($valuehash[$stime.ToString("HH:mm")].FBSubject -ne $null){
if ($valuehash[$stime.ToString("HH:mm")].FBLocation -ne $null){
$title = $title + "`"" + $valuehash[$stime.ToString("HH:mm")].FBSubject.ToString() + " " + $valuehash[$stime.ToString("HH:mm")].FBLocation.ToString() + "`" "
}
else {
$title = $title + "`"" + $valuehash[$stime.ToString("HH:mm")].FBSubject.ToString() + "`" "
}
}
else {
if ($valuehash[$stime.ToString("HH:mm")].FBLocation -ne $null){
$title = $title + "`"" + $valuehash[$stime.ToString("HH:mm")].FBLocation.ToString() + "`" "
}
}
if($title -ne "title="){
$fbBoard = $fbBoard + "<td " + $bgColour + " " + $title + "></td>" + "`r`n"
}
else{
$fbBoard = $fbBoard + "<td " + $bgColour + "></td>" + "`r`n"
}

}
$fbBoard = $fbBoard + "</tr>" + "`r`n"

}
$fbBoard = $fbBoard + "</table>" + " "
$fbBoard | out-file "c:\fbboard.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 Gr...

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 ...

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 ...
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.