Saturday, August 01, 2009

Twitter your Exchange Calendar reminders using Powershell and the EWS Managed API

As twitter is now more a part of the everyday rather than a flash in the pan it does give some new options for solving those time old problems the annoy us all. One of these is missing your meeting reminders or making sure your team or company don’t forget about any important calendar events if you have a company calendar or mailbox. Exchange and Outlook have always had a good functionality for tracking and setting reminders but human nature has something to do with us ignoring or not receiving them.

So how can scripting and a little EWS code solve this well it first has to start with a query of the calendar where the appointments you want to search against are located. Because reminders can be set for 2 weeks before an appointment you need to query for appointments during this time frame. Then filter anything thats set to private and you have some appointments that might have reminders you need to twitter. But before this you first need to use a little maths

x = minutesbeforeappointment or the calendar appointments .ReminderMinutesBeforeStart
y = Appointment StartTime
n = The Current time

so if (y-x) > n and (y-x)< n+15 and the last twitter anouncement doesn't have the same subject Twitter the subject of the appointment with the Start DateTime Attached. This means that if the appointment reminders is due in the next 15 minutes it will be twiitered. eg if the appoinment starts at 8:00 AM and has a 30 minute reminder then between 7:15 and 7:30 if this script was run it would twitter the reminder

Before running this script you need to set the email address of the mailbox where the calendar you want to twitter is located (if its a public folder then you would need to add some code to find that folderID first). And you also need to set the Twitter UserName and Password

eg

$MailboxName = "company@yourdomain.com"
$twitterusername = "username"
$twitterpassword = "password"


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

$MailboxName = "user@domain.com"

$twitterusername = "userName"
$twitterpassword = "password"


function updateTwiterStatus($PostString){

$tweetstring = [String]::Format("status={0}", $PostString )
[System.Net.ServicePointManager]::Expect100Continue = $false
$request = [System.Net.WebRequest]::Create("http://twitter.com/statuses/update.xml")
$request.Credentials = new-object System.Net.NetworkCredential($twitterusername,$twitterpassword)
$request.Method = "POST"
$request.ContentType = "application/x-www-form-urlencoded"
$formdata = [System.Text.Encoding]::UTF8.GetBytes($tweetstring)
$request.ContentLength = $formdata.Length
$requestStream = $request.GetRequestStream()
$requestStream.Write($formdata, 0, $formdata.Length)
$requestStream.Close()
$response = $request.GetResponse()
$reader = new-object System.IO.StreamReader($response.GetResponseStream())
$returnvalue = $reader.ReadToEnd()
$reader.Close()

}

function GetTwitterStatus(){
[System.Net.ServicePointManager]::Expect100Continue = $false
$request = [System.Net.WebRequest]::Create("http://twitter.com/users/show/" + $twitterusername)
$request.Credentials = new-object System.Net.NetworkCredential($twitterusername,$twitterpassword)
$request.Method = "GET"
$request.ContentType = "application/x-www-form-urlencoded"
$response = $request.GetResponse()
$ResponseStream = $response.GetResponseStream()
$ResponseXmlDoc = new-object System.Xml.XmlDocument
$ResponseXmlDoc.Load($ResponseStream)
$StatusNodes = @($ResponseXmlDoc.getElementsByTagName("status"))
$returnStatus = $StatusNodes[0].text
$ResponseXmlDoc.Save("c:\dd.xml")
return $returnStatus.ToString()
}

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)

$windowsIdentity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$sidbind = "LDAP://<SID=" + $windowsIdentity.user.Value.ToString() + ">"
$aceuser = [ADSI]$sidbind

$service.AutodiscoverUrl($aceuser.mail.ToString())

$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName)
$CalendarFolder = [Microsoft.Exchange.WebServices.Data.CalendarFolder]::Bind($service,$folderid)

$cvCalendarview = new-object Microsoft.Exchange.WebServices.Data.CalendarView
$cvCalendarview.StartDate = [System.DateTime]::Now
$cvCalendarview.EndDate = [System.DateTime]::Now.AddDays(14)
$cvCalendarview.MaxItemsReturned = 200;
$cvCalendarview.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)

$upset = 0
$statusup = 0

$frCalendarResult = $CalendarFolder.FindAppointments($cvCalendarview)

foreach ($apApointment in $frCalendarResult.Items){

if ($apApointment.Sensitivity -ne [Microsoft.Exchange.WebServices.Data.Sensitivity]::Private -band $apApointment.IsReminderSet -eq $true){
$ReminderSendTime = $apApointment.Start.AddMinutes(-$apApointment.ReminderMinutesBeforeStart)
if ($ReminderSendTime -ge [System.DateTime]::Now -band $ReminderSendTime -le [System.DateTime]::Now.AddMinutes(15))
{
$twitString = $apApointment.Subject.ToString()
if ($twitString.Length -gt 140){$twitString = $twitString.Substring(0,110)}
$twitString = $twitString + " Starts : " + $apApointment.Start.ToString("yyyy-MM-dd HH:mm:ss") + " "
[String]$currentStatus = GetTwitterStatus
$twitString = $twitString.Substring(0,$twitString.length-1)
write-host $twitString.ToLower().ToString()
write-host $currentStatus.ToLower().ToString()
if ($currentStatus.ToLower().ToString() -ne $twitString.ToLower().ToString()){
if ($statusup -ne 3){$statusup = 1}
$upset = 1
}
else{
$statusup = 3
}
}

}

}
if ($statusup -eq 1){
updateTwiterStatus($twitString)
"Twitter Status Updated"
}
else {"Nothing changed since last update"}

1 comment:

Amano Jyaku said...

How would one accomplish this utilizing Exchange 2003 & OWA? Is it even possible, or is it just a feature only available to Exchange 2007 & higher?

It would be extremely helpfull if you or someone could adapt this code to work with Exchange 2003. Thanks!