Monday, March 12, 2012

EWS Managed API and Powershell How-To Series Part 5 Delegate Operations

This is part 5 of my ongoing EWS and Powershell How-To series in this part I'm going to be looking at the special EWS operations starting with the Delegate operations in this post.

The Tale of Two Delegates (it is the 200th Anniversary of dickens)

When you talk about delegates with EWS it can be a little confusing because it could be your talking about accessing another mailbox as a delegate for example . For example to access another mailboxes Inbox as a Delegate you would use

  1. $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
  2. $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)   
The other type of Delegates are the assigning of Mailbox permissions via the use of the Delegate Operations or to put it pictorially this function in Outlook

Delegates are always the best way of assigning shared rights to users Mailbox and Calendar folders because it always provides accountability to the user eg a user can always go in and see whom their delegates are where as the alternative of assigning rights via Add-MailboxPermission or modifying the folder permission directly may not be a easily visible and accountable to the user.

So first lets look at the code that can be used to get the delegates in a Mailbox

  1. #Get Delegates for Mailbox  
  2. $delegates = $service.getdelegates($MailboxName,$true)  
  3. #Enumerate Delegates  
  4. foreach($Delegate in $delegates.DelegateUserResponses){  
  5.     $Delegate.DelegateUser  
  6. }  
Pretty simple but the response itself is first worth looking at because it contains two distinct data elements, All the information about delegate permissions is held within the DelegateUserResponses collection and the information that controls the Radio buttons under the "Delivery meeting requests addressed to me and responses..." in the above screen shoot is held in a separate property called MeetingRequestsDeliveryScope. This is important when it comes to adding a new delegate.

Adding a New Delegate

  As I've just talked about when you go to add a new delegate you first should request the existing delegates on the Mailbox so you can determine the configuration of the MeetingRequestsDeliveryScope (Otherwise you will risk changing the current configuration) and you can also check if that delegate already exists and just needs to be modified instead. Lets look at a example of adding a delegate to a Mailbox this script will get the existing delegates first check to see if the delegate you want to add already existing and if not adds that delegate and make sure this delegate has Reviewer rights to the Inbox.

  1. $delegatetoAdd = "bob.test@domain.com"  
  2. $exists = $false  
  3.   
  4. $delegates = $service.getdelegates($MailboxName,$true)  
  5. foreach($Delegate in $delegates.DelegateUserResponses){  
  6.     $Delegate.DelegateUser.UserId.PrimarySmtpAddress  
  7.     if($Delegate.DelegateUser.UserId.PrimarySmtpAddress.ToLower() -eq $delegatetoAdd.ToLower()){  
  8.         #Change Inbox permissions  
  9.         $Delegate.DelegateUser.Permissions.InboxFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Reviewer  
  10.         $service.UpdateDelegates($MailboxName,$delegates.MeetingRequestsDeliveryScope,$Delegate.DelegateUser)  
  11.         $exists = $true  
  12.         "Delegate Updated"  
  13.     }  
  14. }   
  15. if($exists -eq $false){  
  16.     $dgUser = new-object Microsoft.Exchange.WebServices.Data.DelegateUser($delegatetoAdd)  
  17.     $dgUser.ViewPrivateItems = $false  
  18.     $dgUser.ReceiveCopiesOfMeetingMessages = $false  
  19.     $dgUser.Permissions.CalendarFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Editor  
  20.     $dgUser.Permissions.InboxFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Reviewer  
  21.     $dgArray = new-object Microsoft.Exchange.WebServices.Data.DelegateUser[] 1  
  22.     $dgArray[0] = $dgUser  
  23.     $service.AddDelegates($MailboxName,$delegates.MeetingRequestsDeliveryScope,$dgArray)  
  24.     "Delegate Added"  
  25. }  

Removing a Delegate

To remove a delegate its pretty similar to adding a new one you should first get the existing delegates then use the removedelegates method eg 

  1. $delegateToRemove = "bob.test@domain.com"  
  2. $delegates = $service.getdelegates($MailboxName,$true)  
  3. foreach($Delegate in $delegates.DelegateUserResponses){  
  4.     $Delegate.DelegateUser.UserId.PrimarySmtpAddress  
  5.     if($Delegate.DelegateUser.UserId.PrimarySmtpAddress.ToLower() -eq $delegatetoAdd.ToLower()){  
  6.         $service.RemoveDelegates($MailboxName,$delegateToRemove)  
  7.         "Removing Delegate"  
  8.     }  
  9. }  
Reporting on Delegates

One of the more useful things to do with delegates is produce a report of the currently configured delegates on a mailbox . This can come in use if you want to send a report to a user whom may have no idea who currently has access to their mailbox. Here's a sample of producing a HTML report of the delegates on a mailbox.

  1. $rptCollection = @()  
  2. $delegates = $service.getdelegates($MailboxName,$true)  
  3. foreach($Delegate in $delegates.DelegateUserResponses){  
  4.     $rptObj = "" | select EmailAddress,Inbox,Calendar,Contacts,Tasks,Notes,Journal,MeetingMessages,ViewPrivateItems  
  5.     $rptObj.EmailAddress = $Delegate.DelegateUser.UserId.PrimarySmtpAddress  
  6.     $rptObj.Inbox = $Delegate.DelegateUser.Permissions.InboxFolderPermissionLevel  
  7.     $rptObj.Calendar = $Delegate.DelegateUser.Permissions.CalendarFolderPermissionLevel  
  8.     $rptObj.Contacts = $Delegate.DelegateUser.Permissions.ContactsFolderPermissionLevel  
  9.     $rptObj.Tasks = $Delegate.DelegateUser.Permissions.TasksFolderPermissionLevel  
  10.     $rptObj.Notes = $Delegate.DelegateUser.Permissions.NotesFolderPermissionLevel  
  11.     $rptObj.Journal = $Delegate.DelegateUser.Permissions.JournalFolderPermissionLevel  
  12.     $rptObj.ViewPrivateItems = $Delegate.DelegateUser.ViewPrivateItems  
  13.     $rptObj.MeetingMessages = $Delegate.DelegateUser.ReceiveCopiesOfMeetingMessages  
  14.     $rptCollection += $rptObj  
  15. }  
  16.   
  17. $tableStyle = @" 
  18. <style> 
  19. BODY{background-color:white;} 
  20. TABLE{border-width: 1px; 
  21.   border-style: solid; 
  22.   border-color: black; 
  23.   border-collapse: collapse; 
  24. } 
  25. TH{border-width: 1px; 
  26.   padding: 10px; 
  27.   border-style: solid; 
  28.   border-color: black; 
  29.   background-color:#66CCCC 
  30. } 
  31. TD{border-width: 1px; 
  32.   padding: 2px; 
  33.   border-style: solid; 
  34.   border-color: black; 
  35.   background-color:white 
  36. } 
  37. </style> 
  38. "@  
  39.     
  40. $body = @" 
  41. <p style="font-size:25px;family:calibri;color:#ff9100">  
  42. $TableHeader  
  43. </p>  
  44. "@  
  45.   
  46. $rptCollection | ConvertTo-HTML -head $tableStyle | Out-File c:\delgateReport.html  
When it all goes wrong

Delegates are one of the things for a number of reasons that can become corrupted or Invalid. The Delegate operations that the  EWS Managed API provides abstracts of the Raw Delegate information which is held in number of properties on the Local Freebusy information the Folder DACL and possibly a Inbox Rule if calendar forwards are enabled. To Access and fix this raw data if something isn't working have a look at this post 

7 comments:

Victor said...

Interesting code. When I run your script for Reporting on Delegates, I've got "Custom" permission level for some delegates/folders. Is it possible to get more precise values?
Thank you.

Anonymous said...

When I try to run the delegate report, I get an Unexpected Token error on 'rptCollection'.

Unknown said...

Hi Glen,
I tried to use the delegation from ews and I succeed. But for me , the result of getDelegates() seems useless.
Instead to find the person that may see my Inbox, I would prefer to have a method that told me what accounts can access myself.
If I want to have access other person Inbox for example, i must keep in mind/on paper that person delegate to me this right. Isn't odd?
on OWA, the URL is manually composed. There is no help from UI to provide the info that I looking for.
Do you know a workaround fro this?
I find this http://hellomate.typepad.com/exchange/2004/09/listing_which_e.html

but the result is bijective and i dont know how to split info in delegates to | delegated from categories.


Thank you in advance.

10cha said...

Everything looks good, I am looking for a bit of a combination. I would like to get list of delegates and verify the "view private" permission.

Anonymous said...

Glen,

I had this script working in Exchange 2013 SP1 and we recently performed an upgrade to Exchange 2013 SP1 CU6 and the script still runs however the delegate user is being added to the user account that is running the Exchange management shell (the administrator who is performing the work).

I have enabled the trace functionality of the EWS service and verified that the "emailaddress" field is populated with the correct user who was entered as the mailbox to add the delegate to (see below).



this displays the correct user's email address

There are no other errors and the delegate is successfully added, however it is just to the wrong mailbox.

Any ideas or advice?

Thank you in advanced.

Glen Scales said...

Looks like a bug I can reproduce it on Office365 my short term workaround would be to use Impersonation which should get around this until they fix the bug

Anonymous said...

Thanks Glen, I'll give the impersonation a try.

Thanks again.