Skip to main content

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 

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 test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m
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.