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
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
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.
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
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.
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
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
- $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)
- $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
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
- #Get Delegates for Mailbox
- $delegates = $service.getdelegates($MailboxName,$true)
- #Enumerate Delegates
- foreach($Delegate in $delegates.DelegateUserResponses){
- $Delegate.DelegateUser
- }
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.
- $delegatetoAdd = "bob.test@domain.com"
- $exists = $false
- $delegates = $service.getdelegates($MailboxName,$true)
- foreach($Delegate in $delegates.DelegateUserResponses){
- $Delegate.DelegateUser.UserId.PrimarySmtpAddress
- if($Delegate.DelegateUser.UserId.PrimarySmtpAddress.ToLower() -eq $delegatetoAdd.ToLower()){
- #Change Inbox permissions
- $Delegate.DelegateUser.Permissions.InboxFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Reviewer
- $service.UpdateDelegates($MailboxName,$delegates.MeetingRequestsDeliveryScope,$Delegate.DelegateUser)
- $exists = $true
- "Delegate Updated"
- }
- }
- if($exists -eq $false){
- $dgUser = new-object Microsoft.Exchange.WebServices.Data.DelegateUser($delegatetoAdd)
- $dgUser.ViewPrivateItems = $false
- $dgUser.ReceiveCopiesOfMeetingMessages = $false
- $dgUser.Permissions.CalendarFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Editor
- $dgUser.Permissions.InboxFolderPermissionLevel = [Microsoft.Exchange.WebServices.Data.DelegateFolderPermissionLevel]::Reviewer
- $dgArray = new-object Microsoft.Exchange.WebServices.Data.DelegateUser[] 1
- $dgArray[0] = $dgUser
- $service.AddDelegates($MailboxName,$delegates.MeetingRequestsDeliveryScope,$dgArray)
- "Delegate Added"
- }
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
- $delegateToRemove = "bob.test@domain.com"
- $delegates = $service.getdelegates($MailboxName,$true)
- foreach($Delegate in $delegates.DelegateUserResponses){
- $Delegate.DelegateUser.UserId.PrimarySmtpAddress
- if($Delegate.DelegateUser.UserId.PrimarySmtpAddress.ToLower() -eq $delegatetoAdd.ToLower()){
- $service.RemoveDelegates($MailboxName,$delegateToRemove)
- "Removing Delegate"
- }
- }
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.
- $rptCollection = @()
- $delegates = $service.getdelegates($MailboxName,$true)
- foreach($Delegate in $delegates.DelegateUserResponses){
- $rptObj = "" | select EmailAddress,Inbox,Calendar,Contacts,Tasks,Notes,Journal,MeetingMessages,ViewPrivateItems
- $rptObj.EmailAddress = $Delegate.DelegateUser.UserId.PrimarySmtpAddress
- $rptObj.Inbox = $Delegate.DelegateUser.Permissions.InboxFolderPermissionLevel
- $rptObj.Calendar = $Delegate.DelegateUser.Permissions.CalendarFolderPermissionLevel
- $rptObj.Contacts = $Delegate.DelegateUser.Permissions.ContactsFolderPermissionLevel
- $rptObj.Tasks = $Delegate.DelegateUser.Permissions.TasksFolderPermissionLevel
- $rptObj.Notes = $Delegate.DelegateUser.Permissions.NotesFolderPermissionLevel
- $rptObj.Journal = $Delegate.DelegateUser.Permissions.JournalFolderPermissionLevel
- $rptObj.ViewPrivateItems = $Delegate.DelegateUser.ViewPrivateItems
- $rptObj.MeetingMessages = $Delegate.DelegateUser.ReceiveCopiesOfMeetingMessages
- $rptCollection += $rptObj
- }
- $tableStyle = @"
- <style>
- BODY{background-color:white;}
- TABLE{border-width: 1px;
- border-style: solid;
- border-color: black;
- border-collapse: collapse;
- }
- TH{border-width: 1px;
- padding: 10px;
- border-style: solid;
- border-color: black;
- background-color:#66CCCC
- }
- TD{border-width: 1px;
- padding: 2px;
- border-style: solid;
- border-color: black;
- background-color:white
- }
- </style>
- "@
- $body = @"
- <p style="font-size:25px;family:calibri;color:#ff9100">
- $TableHeader
- </p>
- "@
- $rptCollection | ConvertTo-HTML -head $tableStyle | Out-File c:\delgateReport.html
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