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

Merge MailboxFolders - how to specify folder path and auth fails #22

Closed danno2021 closed 1 year ago

danno2021 commented 1 year ago

Some background on this:

A nasty bug that Microsoft refuses to fix (they deem it to be 'by design'): Can't use Outlook to move a folder tree (a folder that contains subfolders) from one folder to another in a Shared Mailbox. Get the bogus error 'cannot move/copy folder because it may contain private items'. OWA can't be used because the folder structure on the source/target mailbox is hierarchical and complex. When it prompts with the target, it does an 'expand all' on the folders and it's impossible to locate the folder among thousands of nested folders - no option to avoid the 'expand all' or 'collapse' the folder list.

2 issues: 1) I've tried every possible combination of authentication parameters ('-oauth', '-office365' with or without 'credential (get-credential)', etc. I'm already logged into the MS acct associated with the mailboxes. I either get 'Failed to obtain OAuth token', or 'Error (ThrottledFolderBind): Exception calling "Bind" with "3" argument(s): "The request failed. The remote server returned an error: (401) Unauthorized." Failed to open source message store (mailbox@domain.com) 2) How to specify folder path? The path is not a 'wellknownfoldername'. I tried using a folder path I've used successfully with VBA code: "\\\, but I don't know if that's the proper syntax - can't determine because blocked with the above authentication errors.

David-Barrett-MS commented 1 year ago

You need to configure an application in Azure AD for the OAuth part, please see https://github.com/David-Barrett-MS/PowerShell-EWS-Scripts/wiki/Exchange-Online-OAuth-Configuration

(Get-Credential) will no longer work with Exchange Online because basic auth is now deprecated.

Folder path supports normal syntax (and can start with WellKnownFolderName). If no well known folder included, the path starts from Top of Information Store.

danno2021 commented 1 year ago

I followed the steps to add the authentication parameters as explained in https://github.com/David-Barrett-MS/PowerShell-EWS-Scripts/wiki/Exchange-Online-OAuth-Configuration but I'm getting this error: Processing mailbox

: error occurred during autodiscover: Exception calling "AutodiscoverUrl" with "1" argument(s): "Autodiscover blocked a potentially insecure redirection to https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml. To allow Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) overload." Failed to connect to source mailbox" Both source and target mailboxes are Shared Mailboxes. As described previously, this script is needed to overcome the problem moving folder trees in Shared Mailboxes both in Outlook and OWA.
danno2021 commented 1 year ago

Edited comment above - it was missing key info.

mfabunmi2023 commented 1 year ago

.\Merge-MailboxFolder.ps1 -Copy -WhatIf -SourceMailbox Ekun@E-ssentially.com -TargetMailbox SpecMBXshared11@E-ssentially.com -MergeFolderList @{"WellKnownFolderName.Inbox"= "Inbox"; "WellKnownFolderName.SentItems"= "Sent Items"} -ProcessSubfolders -CreateTargetFolder -Office365 -OAuth -OAuthClientId "e3b3c4df-e041-4613-a66d-535309299948" -OAuthTenantId "01088aa4-1bdc-42ad-a76a-17ef028779fc"

Recently came across this script. I am struggling with the use of OAuth. Can you confirm my syntax is correct. I get a prompt with my O365 name and password. After that i get a permissions request box. Immediately after i accept that, i get the message below;

http://localhost/code?code=0.AV4ApIoIAdwbrUKnahfvAod5_N_Es-NB4BNGpm1TUwkpmUheAAA.AgABAAIAAAD--DLA3VO7QrddgJg7WevrAgDs_wUA9P8oHdoP4nXnu0wN7V78wn0YNHactQO2zqWwWzlfEVr41GcaFKylQcK0p1w5dbRSG0DWUlaU0CD9nC8j1c5OOs0moH0pGZzf4AUk_wNoMpFyc7E_698J2ebLdUmetBl4HiUnKNuFMd7yl5dKHk4lDrVQbfvM5CByL6iHFgS6HFrOlMmtGIgx9vb_R11Tu_AfP3uvzQddSHXzl3wHKbCFuoNF9DomrzNs4uwMFupeorE5kWyWYMMd7YtOjKHBpmXgnFPyKZLZCq-alaBMYtu8iIG68ei44dEVmdlTfp9Q2yh8OFNeThHghEZvyVeZoWtFsmDQYZLBpeLWe2BJq6x9HQ6Y9S-IEnjTOxOPIa9iDXPO7ApOS9G5Wci4PHzdTrtb-cAoL0IpN6xbIjs7FqQcmYEPX9S2x8JSIh4bqNKwjgcuGCe-vXAiDvrma4ywLRNgJiMrexOxufc-oA6EgsRQ2EZO2nNuxrIOSmvcvcYRTK_L9r-NWZPospTtX2wMJk1c6ydziFlIWLCWVaQv26Yl22aUCwUX6il8oo1lKw6QXY0jfkNrEJZNimE5bWtseHajU2QD2DXP9L-mFuslAXfkmVWdG7Ctv5JmNcLrKXxuj0qOpUwoZO3tW9N9uD9zWF_z6fqACnZt4vIiG_xJD85CQYcp8ANs1UTsiLS8cIzlrGIZZBcPaWv9GuQuHlNm4dLYOaxhVB8RTD_W1adDIg1Zzt5Z9xGokQSB0XWxYo_eF7EogUZdbfbKZ9mqhappGlJAxUUi3B2ZwaLE16X_ci0dg_nmywMwRSbP-OAkpt2NdpILlUY8RzZv1bqhIg7C&session_state=8ab80853-963c-4334-843b-0eb943598c33

The machine i am running the script on is running Exchange 2013 on Windows 2008R. I am using this server because i could not get the Microsoft.Exchange.WebServices.dll to register correctly on my Windows 10 machine. Could it be linked to the RedirectURL (http://localhost/code) - as the machine is already running IIS?

David-Barrett-MS commented 1 year ago

For the AutoDiscover error, you can avoid that by using -Office365 switch (autodiscover is not needed for Exchange Online as there is a single EWS endpoint). You could also use the -AllowInsecureRedirection switch with the script to allow it to follow the redirection (which per the log, is to Office 365 so is expected).