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

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

How to access and restore deleted Items (Recoverable Items) in the Exchange Online Mailbox dumpster with the Microsoft Graph API and PowerShell

As the information on how to do this would cover multiple posts, I've bound this into a series of mini post docs in my GitHub Repo to try and make this subject a little easier to understand and hopefully navigate for most people.   The Binder index is  https://gscales.github.io/Graph-Powershell-101-Binder/   The topics covered are How you can access the Recoverable Items Folders (and get the size of these folders)  How you can access and search for items in the Deletions and Purges Folders and also how you can Export an item to an Eml from that folder How you can Restore a Deleted Item back to the folder it was deleted from (using the Last Active Parent FolderId) and the sample script is located  https://github.com/gscales/Powershell-Scripts/blob/master/Graph101/Dumpster.ps1

Using the MSAL (Microsoft Authentication Library) in EWS with Office365

Last July Microsoft announced here they would be disabling basic authentication in EWS on October 13 2020 which is now a little over a year away. Given the amount of time that has passed since the announcement any line of business applications or third party applications that you use that had been using Basic authentication should have been modified or upgraded to support using oAuth. If this isn't the case the time to take action is now. When you need to migrate a .NET app or script you have using EWS and basic Authentication you have two Authentication libraries you can choose from ADAL - Azure AD Authentication Library (uses the v1 Azure AD Endpoint) MSAL - Microsoft Authentication Library (uses the v2 Microsoft Identity Platform Endpoint) the most common library you will come across in use is the ADAL libraries because its been around the longest, has good support across a number of languages and allows complex authentications scenarios with support for SAML etc. The
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.