Sunday, March 27, 2011

Dealing with Invalid Delegates with Exchange Web Services and Powershell

Invalid delegates have always been a tricky issue in Exchange an Invalid delegate happens when a mailbox that has a delegate setup and the delegate user account is deleted or disabled(has the Exchange attributes removed). When your using the EWS delegate operations and a mailbox has an invalid delegate you get a message back to tell you the delegate doesn't map to an active directory user however it doesn't tell you who that user was and what type of delegate access that user had. You also can't use the EWS delegate operations to remove a user in this state so you end up with a kind of stalemate solution that requires a manual delete with Outlook. Before going any further its worth covering briefly what happens when you add a delegate to mailbox within Oultook there are three areas of the delegator's mailbox that gets modified.
  • Local freebusy object in the mailbox
  • Permissions on folders that are given access to
  • Rules collection if forward meeting requests option selected
The last things that gets changed is the publicDelegates active directory property.

What happens to the LocalFreeBusy object is of the most interest and has been documented in the http://msdn.microsoft.com/en-us/library/cc425488%28v=exchg.80%29.aspx protocol document basically it comes down to the following 7 Mapi properties on the LocalFreeBusy object.

PidTagScheduleInfoDelegatorWantsInfo
PidTagScheduleInfoDelegatorWantsCopy

PidTagScheduleInfoDelegateNamesW
PidTagScheduleInfoDelegateEntryIds
PidTagDelegateFlags

These last three properties contain specific setting for each delegate user including the X500DN of the delegated users within PidTagScheduleInfoDelegateEntryIds (Its stored as Addessbook entryid).

If your observant you would have only counted 5 properties which is all that are documented in the protocol document. If you look at the object with a Mapi editor however you would discover two more undocumented properties that also hold information about delegates.

x6870 and x6871

To get the localfreebusy object in a mailbox using EWS there are a few methods you can use the first is to search the NON_IPM_Subtree for and find the FreeBusy Data folder and then search that folder for an item with a subject of localfreebusy. The other option is to use is the PR_FREEBUSY_ENTRYIDS extended property on the Non_IPM_Subtree which contains an array of FreeBusy EntryID's which the 2nd element is the LocalFreeBusy Object which you can convert into a EWSid using a convertid operation and then bind directly to that item.

So if your using EWS and you get "The delegate does not map to a user in the Active Directory" and you want to find out which user this refers to you can compare whats returned in PidTagScheduleInfoDelegateNamesW with the valid delegates you back with normal operation you should be able to work out the invalid delegate.

To demonstrate this I've put some Powershell cmdlet's together so basically you can display firstly the Raw property values, show what is invalid doing the compare i talked about above and then lastly you can do a unsupported remove which basically will remove the invalid entries out of the extended properties. The reason i said this is unsupported because its not really tested and its also only modifies the raw properties so all the other things like mailbox permission, forward rules will still be in-place (although I've also created some cmdlets to clean up invalid forward rules as well that I'll talk about in a separate post).

Get-MessageOps.MailboxDelegatesRaw : This Cmdlet retrieves the delegates from a mailbox by reading the Raw Mapi properties from the Local Freebusy Item in a mailbox.

eg Get-MessageOps.MailboxDelegates –p $ewsprofile –identity mailbox@domain.com



Get-MessageOps.MailboxDelegatesInvalid : This Cmdlet retrieves the delegates from a mailbox by reading the Raw Mapi properties from the Local Freebusy Item in a mailbox and then compares them against what is retrieved using a normal getdelegate operation.

eg. Get-MessageOps.MailboxDelegatesInvalid –p $ewsprofile –identity mailbox@domain.com

Remove-MessageOps.MailboxDelegatesInvalid.Unsupported : This Cmdlet retrieves the delegates from a mailbox by reading the Raw Mapi properties from the Local Freebusy Item in a mailbox and then compares them against what is retrieved using a normal getdelegate operation. It then modifies the Raw Mapi properties on the local freebusy object and removes any entries that it finds are invalid. Note this doesn’t remove the delegate forwarding rules if it exists and also doesn’t remove any delegate permissions if they have been set.

eg. Remove-MessageOps.MailboxDelegatesInvalid.Unsupported –p $ewsprofile –identity mailbox@domain.com

To use these cmdlets you need to download the following module http://www.messageops.com/downloads/MessageOps-Exchange-Module.zip

4 comments:

Anonymous said...

Hi Glen,

Great post, this is just what im looking to do.

I am getting results from Get-MessageOps.MailboxDelegates

however the "compare" cmdlets you have mentioned do not seem to be available. e.g.

Get-MessageOps.MailboxDelegatesRaw : Object reference not set to an instance of an object.

Anonymous said...

Hi Glenn, The module doesn't seem to work with the EWS mAPI 1.2.

Glen Scales said...

You need to change the path in the installed script for 1.1 to 1.2 and the rest should work okay. Or manually copy the Managed api dll's to the module directory

Cheers
Glen

Unknown said...

Hey Glen,

i know its 5 years ago :-) i tried to install and import the modules here but get an error after firing the import-module string, stating that the
"file or assembly "Microsoft.Exchange.WebSerbices, Version=14.0.0.0, Culture=neutral,PublicKeyToken=31nf3856ad364e35" or a dependency hasn't found. the system could not find the file."
i installed the current 2.2 API, changed the path and the "install-module" part went a ok.
i'm on a 2013 exchange in hybrid configuration - any ideas where the problem lies? should i try to find a 1.0 version of the API?

thanks in advance!
Mark