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-MailboxFolder.ps1 - failed to obtain token #24

Closed mfabunmi2023 closed 1 year ago

mfabunmi2023 commented 1 year ago

Hi David, I get an error message at the point when i paste in the URL required to generate the authentication code. Can you confirm what value is meant by "paste the redirect URL (including auth code)"? Is it not the value generated in the browser window? Would appreciate any urgent input on this. MergeFolderIssue.docx

PS C:\temp\Specs> .\Merge-MailboxFolder.ps1 -Copy -WhatIf -SourceMailbox Ekun@E-ssentially.com -TargetMailbox SpecMBXshared11@E-ssentially.com -MergeFolderList @{"WellKnownFolderName.Inbo x"= "Inbox"; "WellKnownFolderName.SentItems"= "Sent Items"} -ProcessSubfolders -CreateTargetFolder -Office365 -OAuth -OAuthClientId "e3b3c4df-e041-4613-a66d-535309299948" -OAuthTenantId "01088 aa4-1bdc-42ad-a76a-17ef028779fc" Merge-MailboxFolder.ps1 version 1.2.5 starting Using EWS Managed API found at: C:\Program Files\Microsoft\Exchange Server\V15\Bin\Microsoft.Exchange.WebServices.dll

Processing mailbox Ekun@E-ssentially.com Please complete log-in via the web browser, and then paste the redirect URL (including auth code) here to continue Auth code: http://localhost/code?code=0.AV4ApIoIAdwbrUKnahfvAod5_N_Es-NB4BNGpm1TUwkpmUheAAA.AgABAAIAAAD--DLA3VO7QrddgJg7WevrAgDs_wUA9P-nkH3P6arHA3BUNwmWFwc7-soYGZJoHnD8ryRMEutMSuVQwljMiMrELlOf g2s6jvQ2LFtMrxVDymOL9K3t2UkuVJXM22xmqADVKqWWeho-Jx5YfcRQmkHszNcT3e9SABRUIc9Tas_CvGFGTozk7BibtSVi-Fuk_q8sApFcaHoHXfOtq5lyTHKxbLjw0OZfSlrw6G7jayvd_90X15CXEJMtrioybLjhYRjqRkU0t4NfD1sX32oQpdymNok0 O0vZuEr3zexk_xoCbCEGvkvr3To9EsgzayaaoFWLOlxo9Reji_Su1fM46ga1q6VH__M-EkFW-jfE3QKxwipNOrHTQO6g2yXblv08WDO372SQGi35e5ESKovZv-S8GWHUQB8VJC3raixe92Hms9F_5E2yKCwfWt4BJF__v7VV7WaYymIz5ydvDq73KvCwFJtn JuBM5vHvu8Qr1aeLAspBLOlxVPfhxli_oHyeUd-W5O0ZnnEEtPUm23RPp6HD6hG0vZ-MB3oaOwUGMHRzxwRpVX3C7Qq4TO4GNNMqwuoHTJTQ6ETxVSTp4Ix71KUsk9duQ0-ShjJWgpMEZ9Gjir6y-h04mQrV9dhBNfTOLriiK3Z6NIn5Yipi-Pdn4jABhIjO emi0lzJEX4bLQgzg1HtdDhyECa223XhGGXKzV0vKqAIhOJ_7Tsdf_wSJA-RYesoZmFhvmf2BbBAG5vY6Aoy-jbezbO2fVfKeoIf5K_FECv5lXY1we5nvKVvS1OHAftEfqBpZewXmme7tc1yBGSXXh7nHJQOiB_FzS66cm-rwxpO9apjd8LR81l7UQUaepg&s ession_state=a9adf59b-b7df-4a67-8e84-a7be47946d9b Failed to obtain OAuth token

David-Barrett-MS commented 1 year ago

You have pasted the correct information. The script takes the code (which it can extract from the URL) and then uses that code to obtain a token to access the mailbox. The error above means that the token acquisition part is failing. I haven't implemented anything to trace this part of the script, but you could use Fiddler to capture what is happening. I expect an error is being returned on token acquisition - likely cause is a policy block of some kind, and if so that should be evident in the response.

mfabunmi2023 commented 1 year ago

Could the error be related to running the script on an Exchange Server? Any notes on correctly registering the EWS DLL on a vanilla Windows 10 workstation? Does the bit "http://localhost/code?code" expect IIS to be installed on the machine the script is running on? Cheers

David-Barrett-MS commented 1 year ago

The error is purely to do with auth (and something blocking it). Note that you don't need to register the EWS Managed API. For the script to use it, the dll just needs to be available (I put a copy in the same folder as my scripts). It is also commonly installed with Excel, and the script often picks up that location.

mfabunmi2023 commented 1 year ago

Made some progress and managed to get the script running.

1) I had to follow the instructions @ https://eightwone.com/2020/10/05/ews-webservices-managed-api/, in order to get Microsoft.Exchange.WebServices.dll to be picked up by the script on my Windows 10 machine.

2) The command below worked to mirror all the contents of my source mailbox to the target mailbox;

.\Merge-MailboxFolder.ps1 -SourceMailbox SourceMBX01@MyCompany.com -TargetMailbox TargetMBX01@MyCompany.com -ProcessSubfolders -CreateTargetFolder -Office365 -OAuth -OAuthTenantId "01088aa4-1bdc-42ad-xYYx-17ef028779fc" -Copy

3) I have a few 100 mailboxes to process. Question; How do i use a CSV file with source and target SMTP headers in it? The format of the CSV file and how it is referenced is not too clear in the script. Below is the kind of sample used by the MigrationWiz tool;

PrimarySMTPAddressSource;PrimarySMTPAddressTarget OldMBX01@MyCompamy.com;NewMBX01@MyCompamy.com OldMBX02@MyCompamy.com;NewMBX02@MyCompamy.com OldMBX03@MyCompamy.com;NewMBX03@MyCompamy.com

SampleInputCSV-merge.csv

I see the section below in the script, but it is not clear how to specify $TargetMailbox for each $SourceMailbox entry in the CSV file.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Check whether we have a CSV file as input...

$FileExists = Test-Path $SourceMailbox If ( $FileExists ) {

We have a CSV to process

LogVerbose "Reading mailboxes from CSV file"
$csv = Import-CSV $SourceMailbox -Header "PrimarySmtpAddress"
foreach ($entry in $csv)
{
    LogVerbose $entry.PrimarySmtpAddress
    if (![String]::IsNullOrEmpty($entry.PrimarySmtpAddress))
    {
        if (!$entry.PrimarySmtpAddress.ToLower().Equals("primarysmtpaddress"))
        {
            $SourceMailbox = $entry.PrimarySmtpAddress

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

gkosmas commented 1 year ago

This worked perfectly for me.

Thank you @mfabunmi2023

David-Barrett-MS commented 1 year ago

With regards to: Question; How do i use a CSV file with source and target SMTP headers in it? This isn't currently implemented. I'll raise a new issue for this and add it when I get a chance.