Sunday, August 23, 2009

Setting the Out of Office (OOF) with powershell and the EWS Managed API

Along with the RC of Exchange 2010 this week the RC of the EWS Managed API made its debut. There are a number of changes some of which break a few of the scripts i've posted here there is full list of the changes on http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/7053c628-9227-4cd5-ba9d-ed6fe8d484cb. But the good thing is now OOF and Freebusy work and the DNS lookup for autodisover is another major improvement.

Setting the OOF is pretty easy with the API lets look at a few examples as before you first need to create a Service object and authenticate if you haven't used the EWS Managed API in Powershell before see this post as a primer. To use this OOF code you first need to download and install the RC version of the ManagedAPI from here.

So lets take it as read we have our $service object now to get the Offsettings its just one call with the email address of the Account you want to pull the settings for.

$oofSetting = $service.GetUserOofSettings("user@domain.com")

A good point to remember when using any of the availability service Call like OOF and FreeBusy you need to always use delegate Access as EWS impersonation doesn't work with the availability service.

To show the OOF state just look at the State property

$oofSetting.State.ToString()

To show the oof message you have to look at the InternalReply or ExternalReply properties

$oofSetting.InternalReply.ToString()
$oofSetting.ExternalReply.ToString()

To set the State and or the Message property you should first get the current setting modify the property you want to modify and then update the OOF.

eg to set the OOF of the currently logged on user you could use the following code.



$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())

$oofSetting = $service.GetUserOofSettings($aceuser.mail.ToString())
$oofSetting.State.ToString()
$oofSetting.InternalReply = new-object Microsoft.Exchange.WebServices.Data.OofReply("Test 123")
$oofSetting.ExternalReply.ToString()
$oofSetting.State = [Microsoft.Exchange.WebServices.Data.OofState]::Enabled
$service.SetUserOofSettings($aceuser.mail.ToString(), $oofSetting);

2 comments:

שמעון כהן said...

i am getting this error message

Exception calling "GetUserOofSettings" with "1" argument(s): "Microsoft.Exchange.Data.Storage.AccessDeniedException: Us
er is not mailbox owner. User = S-1-5-21-2573305727-3136801584-897811289-5767, MailboxGuid = S-1-5-21-2573305727-313680
1584-897811289-4798 ---> User is not mailbox owner. User = S-1-5-21-2573305727-3136801584-897811289-5767, MailboxGuid =
S-1-5-21-2573305727-3136801584-897811289-4798"
At C:\WINDOWS\system32\WindowsPowerShell\v1.0\profile.ps1:1014 char:39
+ $oof = $service.GetUserOofSettings <<<< ($Identity);
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

Glen Scales said...

You have to have Full access to a mailbox to be able to use this to get a Users Oof setting. If you have Exchange 2010 and your just trying to get another users OOF setting then you can use MailTips instead eg http://gsexdev.blogspot.com.au/2011/11/using-mailtips-in-ews-to-get-oof-out-of.html

Cheers
Glen