grahamr975 / EWS-Office365-Contact-Sync

Uses Exchange Web Services to synchronize a Global Address List in Office 365 to a user's mailbox
MIT License
96 stars 21 forks source link

Script stops synching contacts after 4-5 minutes #44

Closed matssss closed 3 years ago

matssss commented 3 years ago

Hi,

Currently I'm trying to synchronize our GAL (around 3000 contacts) to our users contacts folder (a custom one) but it always stops after the letter "G" (which takes 4-5 minutes) to get there. The script doesn't crash but just goes to the next "mailbox" in the $MailboxList and then start importing/updating the same amount of contacts. I don't see any error in the logs.

Do you have any idea what can cause this issue? I tried to run the same script on a VM but I'm getting experiencing the same issue there.

Kind regards, Mats

matssss commented 3 years ago

Hi,

Seems like it stops after I hit the 1000 contacts limit. Do you have any workaround for this (e.g. create/import in a second contact folder after it counted 1000 contacts)?

Thanks a lot!

Kind regards, Mats

grahamr975 commented 3 years ago

Hello Mats,

I'm able to work with over 1000 contacts in my environment just fine. If there's a terminating error with a single contact, the script will just skip to the next contact, not go to the next mailbox. Secondly, I'm not sure what you mean by the script moving to the next mailbox at "G" since it does not import in alphabetical order. Perhaps not of your contacts are being pulled in? Can you try to add the below line to EWSContactSync.PS1 to generate a file with all of your contacts to make sure there's actually 3000?

# Fetch list of Global Address List contacts using Office 365 Powershell
$GALContacts = Get-GALContacts -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credentials $Credential -ExcludeContactsWithoutPhoneNumber $ExcludeContactsWithoutPhoneNumber -ExcludeSharedMailboxContacts $ExcludeSharedMailboxContacts -IncludeNonUserContacts $IncludeNonUserContacts

$GALContacts | Export-Csv -Path .\MyContacts.csv -NoTypeInformation # ADD THIS LINE HERE
matssss commented 3 years ago

Hi Grahamr975,

Thanks for the reply!

I removed the last imported contact and when I rerun the script the removed contact gets deleted and the first following contact gets added into the contacts folder. When I readd the removed user, the newly added user gets deleted again and the "last" user gets added again. It seems like it just stops around 1000 contacts - I think the guy in the following article ran into the same problem: https://docs.microsoft.com/en-us/answers/questions/281934/powershell-script-on-exchange-server-returns-800-r.html

I included the export-csv line but it outputs something else than a contact-list. 2021-08-03 17_01_05-Window

Kind regards, Mats

matssss commented 3 years ago

Hi Grahamr975,

I have noticed that the "fix" in the link I shared is already implemented in your script so that can't be the problem. Today I tried to upper the throtthling limit and rerun the script on a new account in a new directory but unfortunately it keeps stopping after the amount of contacts.

Do you have any more troubleshooting steps?

Thanks a lot!

Kind regards, Mats

grahamr975 commented 3 years ago

Please try using the below line to generate a CSV file of contacts instead of the previous one I sent you.

$GALContacts | Select-Object WindowsEmailAddress | Out-File -FilePath ".\MyContacts.csv"

matssss commented 3 years ago

Hi Grahamr,

The content I get in my export is exactly the amount of contacts that always gets imported. 2021-08-05 08_38_05-export

Kind regards, Mats

matssss commented 3 years ago

Hi Grahamr,

Something I noticed today while running the script: WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items returned. To return all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual number of items, returning all items can take a long time and consume a large amount of memory. Also, we don't recommend storing the results in a variable. Instead, pipe the results to another task or script to perform batch changes.

I checked the functions but don't really where the above is missing. Do you have any idea?

Thanks a lot!

Kind regards, Mats

grahamr975 commented 3 years ago

Hello Mats,

Thanks for generating the contact list. The CSV shows us that the script is only fetching approximately 1000 contacts from your environment's Global Address List. This helps us narrow down the issue. Can you please verify that line 53 of Get-GALContacts.ps1 contains -ResultSize unlimited? It should be the below, but perhaps you may have accidentally remove part of the line.

$ContactList = Get-User -ResultSize unlimited

Secondly, where are you getting this 3000 number from? This script only fetches Azure users in your environment. It does not include on-premise only users at this time. Can you try including -IncludeNonUserContacts as a flag in the script? This additionally syncs contacts that aren't actual mailboxes.

matssss commented 3 years ago

Hi Grahamr,

I checked line 53, it contains "-ResultSize unlimited".

Here you see a screenshot of the amount of mail contacts: 2021-08-05 16_42_26-contacts - Microsoft Exchange

When I set -IncludeNonUserContacts to $false, I only get around 60 contacts, which is right.

Kind regards, Mats

grahamr975 commented 3 years ago

Mats,

I found a bug with the code and applied hotfix bcbcaea11415cc3e74a1a0de45cc2e9fc9757bd5 because when using the -IncludeNonUserContacts flag, only up to 1000 additional non-user contacts could be returned. Can you please try to download the latest version and let me know if this resolves your issue? Thanks

matssss commented 3 years ago

Hi Grahamr,

Thanks a lot, it works now!

Thanks for troubleshooting this so quickly. Have a great day!

Kind regards, Mats