David-Barrett-MS / PowerShell-EWS-Scripts

A collection of PowerShell scripts that use EWS to perform actions against Exchange mailboxes
MIT License
118 stars 13 forks source link

MergeFolderList fails with "Default folder Drafts not found" #55

Closed CSchulz closed 3 months ago

CSchulz commented 3 months ago

I have two default created mailboxes but with different languages. My wish was to merge automatically WellKnownFolderName.Drafts into WellKnownFolderName.Drafts without caring about the used translation.

Target: image

Source: image

But the script fails.

.\Merge-MailboxFolder.ps1 -SourceMailbox test@schulzolutions.de -TargetMailbox kontakt@schulzolutions.de -ProcessSubfolders -CreateTargetFolder -Copy -Office365 -Impersonate -OAuth -MergeFolderList @{"WellKnownFolderName.Drafts" = "WellKnownFolderName.Drafts"}
11.06.2024 17:36:11 [Merge-MailboxFolder.ps1] Merge-MailboxFolder.ps1 version 1.4.2 starting
11.06.2024 17:36:11 [LoadEWSManagedAPI] Using EWS Managed API found at: PowerShell-EWS-Scripts\Legacy\Microsoft.Exchange.WebServices.dll

Processing mailbox test@schulzolutions.de
11.06.2024 17:36:12 [ErrorReported] ERROR (ThrottledFolderBind): Ausnahme beim Aufrufen von "Bind" mit 3 Argument(en):  "The specified object was not found in the store., Default folder Drafts not found."
Merge parameters invalid: merge  into WellKnownFolderName.Drafts
Finished processing mailbox test@schulzolutions.de
11.06.2024 17:36:12 [Merge-MailboxFolder.ps1] Script finished in 00:00:00.3770003

I have also tried to use the correct folder names which fails like this:

[...]-MergeFolderList @{"Entwürfe" = "Drafts"}
[...]
11.06.2024 17:35:30 [GetFolder] Failed to create folder Entwürfe in path Entwürfe
Merge parameters invalid: merge  into Entwürfe

Also tried to use WellKnownFolderName.Drafts in both cases:

[...]-MergeFolderList @{"WellKnownFolderName.Drafts" = "WellKnownFolderName.Drafts"}
[...]
11.06.2024 17:34:50 [ErrorReported] ERROR (ThrottledFolderBind): Ausnahme beim Aufrufen von "Bind" mit 3 Argument(en):  "The specified object was not found in the store., Default folder Drafts not found."
Merge parameters invalid: merge  into WellKnownFolderName.Drafts
David-Barrett-MS commented 3 months ago

Does source mailbox have FullAccess rights to target mailbox? The script impersonates the source mailbox, so that mailbox needs FullAccess rights to the target for the impersonation to work.

CSchulz commented 3 months ago

Yes, because it is possible that the source mailbox is able to create new folders. But it seems that the script isn't able to retrieve the WellKnownFolderName bindings.

I have found an interesting point with the permissions. I was expecting giving permissions on highest level is enough, but it seems that I also have to set the permission on each folder (red marked). Isn't it set recursive automatically if I set it on the root element?

image

David-Barrett-MS commented 3 months ago

Folder permissions won't work in this scenario. FullAccess permissions are needed (https://learn.microsoft.com/en-us/exchange/recipients-in-exchange-online/manage-permissions-for-recipients).

I have just tested this - I can reproduce the issue when I don't have permissions set, but after granting FullAccess the script then works as expected.

CSchulz commented 3 months ago

Works fine for me as well.

Perhaps you can update the documentation to mention this as well. I have found within another issue the hint about the permissions, but I didn't got that there are differences between folder permissions and mail box permissions.

CSchulz commented 3 months ago

I have two mailboxes, one works with the mailbox permission update and the other does not work. 🤣

11.06.2024 18:19:01 [MoveItems] Copying from source:\Top of Information Store\Drafts to target:\Oberste Ebene des Informationsspeichers\Entwürfe
11.06.2024 18:19:01 [MoveItems] 14 items found; attempting to copy
11.06.2024 18:19:01 [ThrottledBatchMove] UNEXPECTED ERROR ON MOVE: Ausnahme beim Aufrufen von "CopyItems" mit 3 Argument(en):  "The specified target folder could not be found."

I have set the permission like for the other mailbox:

Add-MailboxPermission -Identity target -User source -AccessRights FullAccess -InheritanceType All -AutoMapping $false

EDIT: It seems like the Draft folder isn't known / mapped correctly. Using -MergeFolderList @{"Entwürfe" = "Drafts"} worked.

David-Barrett-MS commented 3 months ago

I'll do some testing for the scenario, it's not one I've tested before. I suspect the script may be targeting one of the WellKnownFolders at the wrong mailbox (from the log, this would be the target mailbox).

For the mailbox where it worked, was the localisation of each of the mailboxes the same? I'm not sure why it would work for one but not another - if the targeting is wrong, I'd expect it to fail consistently (I've only been testing in -WhatIf mode so far).

CSchulz commented 3 months ago

Here both scenarios:

Source Target
Entwürfe Drafts Worked with WellKnownFolderName
Drafts Entwürfe Worked with with manual mapping

Some side note, the mailbox had only issues with the Drafts WellKnownFolderName. The others worked directly. Perhaps there is also some issue at our O365 Tenant, I can't explain.

Finally it is working. Thanks for the support and your script!

David-Barrett-MS commented 3 months ago

If it worked for the other WellKnownFolders then the script must be targeting ok (I thought it should be as I did some work quite some time ago to ensure mailbox was included with folder Ids).

If the issue only occurred with one Drafts folder, I suspect mailbox corruption. The Drafts folder is defined by a property on the mailbox root folder (PR_IPM_DRAFTS_ENTRYID) - I have seen this kind of issue when that property is invalid (e.g. the EntryId does not match a folder).

Thanks for confirming - I won't test further unless I get any other reports of issues.