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

Feature Request: Add parameter to not delete old contacts #12

Closed teconmoon closed 4 years ago

teconmoon commented 4 years ago

Hi there,

I am wondering if a parameter can be added so that when called, the script doesn't delete contacts that don't exist in the GAL?

A little background, our organization for a few reasons uses the Outlook app exclusively on all phones, with the contact sync feature turned on. This feature unfortunately only syncs from the default Contacts folder, not any custom folders. Running the script against the contacts folder results in any custom contacts being bulldozed over.

A workaround I implemented myself was to simply change Line 63 of Sync-ContactList.ps1 to $MailboxContactsToBeDeleted = $null.

The end result is that custom contacts are retained, and actual employees are kept up-to-date with the latest information. This was pretty easy to communicate to employees what to expect.

Anyways, if this could be an official parameter to not delete contacts, that would be great. My worry is updating to a newer release in the future and forgetting about that line, and end up forgetting to reset it and deleting people's contacts.

Thanks for reading!

grahamr975 commented 4 years ago

The script needs the ability to automatically delete contacts. We need this ability for two reasons:

  1. If a user is removed from your directory, remove the user's contact
  2. If a user in your directory changes their email address, remove the user's old contact to prevent duplicates

Disabling delete will remove the above features. I would not advise this. How are you creating these custom contacts? Can you add instead add your custom contacts as users in your GAL? Try to run the script with the -IncludeNonUserContacts flag.

teconmoon commented 4 years ago

By custom contacts I more meant random contacts created by the end users, usually for people outside our organization that they work with. Not contacts maintained by our IT Staff.

If a parameter to bypass deletion is out-of-scope for what this script is aiming to accomplish, I understand.

grahamr975 commented 4 years ago

Ok, I understand. You want to make sure user-created contacts won't be deleted. Unfortunately, I won't be adding your requested parameter because it would break the script's syncing functionality. I've included the reasons why below. Sorry for the inconvenience.

The script needs the ability to automatically delete contacts. We need this ability for two reasons:

  1. If a user is removed from your directory, remove the user's contact
  2. If a user in your directory changes their email address, remove the user's old contact to prevent duplicates
x-zempt commented 3 years ago

@teconmoon I stumbled across this issue you posted while searching to see if I could find a resolution for another issues I'm having.

I've adjusted the script to do just this for our company. Basically, when it is checking for contacts to delete, I've added a rule to only delete ones that are not in the GAL and ALSO are of our domain only. So it will update and remove our business contacts from GAL. But won't touch the end users external contacts.

It has some hardcoded internal data at the moment, but I'll see if I can remove the hardcoded stuff and use variables and put a pull request in, or maybe a fork if needed.

Edit:

Actually, this is what I've done, we have two company domains that we use for email addresses. you can see where I've put domain1.com and domain2.com Just replace those with your email domains.

$MailboxContactsToBeDeleted = $MailboxContacts | Where-Object {((($_.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Address.ToLower() -like "*domain1.com*") -or ($_.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Address.ToLower() -like "*domain2.com*")) -and (!$ContactList.WindowsEmailAddress.ToLower().Contains($_.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Address.ToLower())))}
$MailboxContactsToBeDeleted.count
$MailboxContactsToBeUpdated = $ContactList | Where-Object {$MailboxEmailList.Contains($_.WindowsEmailAddress)}
$MailboxContactsToBeUpdated.count
$MailboxContactsToBeCreated = $ContactList | Where-Object {!$MailboxEmailList.Contains($_.WindowsEmailAddress)}
$MailboxContactsToBeCreated.count

Remove the below if you only have one domain for your emails, add additional if you have more.

-or ($_.EmailAddresses[[Microsoft.Exchange.WebServices.Data.EmailAddressKey]::EmailAddress1].Address.ToLower() -like "*domain2.com*")