Saturday, December 20, 2008

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"