EvotecIT / O365Synchronizer

O365Synchronizer is a PowerShell module that allows you to synchronize users/contacts to user mailboxes contact list. It can also be used to synchronize users between tenants as contacts or guests.
23 stars 5 forks source link

Is it possible to sync to a subfolder #1

Closed dapf27 closed 1 day ago

dapf27 commented 7 months ago

Hi, I would like to use your nice module, but it would be nice to sync all that many contacts to a subfolder so it would be easier for the users to differentiate which are their own contacts and which are out of GAL. I saw in your code that the "ParentFolderId" is there, but would it be possible to use a param at the "Sync-O365PersonalContact" to create or update such a subfolder? Thank you!

PrzemyslawKlys commented 6 months ago

Probably there is, but I've not played with it. It would require additional logic, checking if folder exists, creating it and so on. Doable, but requires some work.

IE-MP commented 5 months ago

Would love that functionality as well, if it's in your plans.

PrzemyslawKlys commented 4 months ago

I need to sit down to it and fix it. So ye, it's in plans.

PrzemyslawKlys commented 4 weeks ago

This feature will be in new release. Ability to sync to a folder. The folder will be created before creating contacts. This release also allows to use filters.

Sync-O365PersonalContact -UserId 'przemyslaw.klys@test.pl' -MemberTypes 'Contact', 'Member' -GuidPrefix 'O365Synchronizer' -PassThru {
        Sync-O365PersonalContactFilter -Type Include -Property 'CompanyName' -Value 'Evotec*','Ziomek*' -Operator 'like'
        Sync-O365PersonalContactFilterGroup -Type Include -GroupID 'e7772951-4b0e-4f10-8f38-eae9b8f55962'
} -FolderName 'O365Sync' -WhatIf | Format-Table

Please keep in mind that if you are switching from non-folder sync you first need to remove old syncs, or you will get sort of duplicates.

I've also added ability to clear contacts from that folder, and clear & delete the folder itself.

Clear-O365PersonalContact -Identity 'przemyslaw.klys@test.pl' -GuidPrefix 'O365Synchronizer' -FolderName 'O365' -WhatIf

Clear-O365PersonalContact -Identity 'przemyslaw.klys@test.pl' -GuidPrefix 'O365Synchronizer' -FolderName 'O365Sync' -FolderRemove -WhatIf

Of course please make sure to use WhatIf, testing to rule out potential bugs.

dapf27 commented 3 weeks ago

Thanks for your work. But I think it destroyed some things. Work good:

Don't work:

PrzemyslawKlys commented 3 weeks ago

did you remove the old contacts first before introducing folders?

PrzemyslawKlys commented 3 weeks ago

Updating and deleting contacts is based on unique id, if you are using same ID and guidprefix there will be duplicates and it will try to update in the main folder

dapf27 commented 3 weeks ago

Yes before starting it was all clean. I had now, that the update worked and at next run it didn't work, some users were updated and some got a duplicate. Running two times this: Sync-O365PersonalContact -UserId '@' -MemberTypes 'Member' -GuidPrefix 'O365Synchronizer' -FolderName 'O365Sync' It shouldn't create duplicates. The Clear-O365PersonalContact deletes only 10 contacts per run. And if I use the -FolderRemove parameter it says: Failed to remove folder O365Sync from @ because: [ErrorCannotDeleteObject] : Object cannot be deleted.

PrzemyslawKlys commented 3 weeks ago

I'll take a look

PrzemyslawKlys commented 2 weeks ago

This should fix it. 1.0.1. I forgot using -All switch which I guess would cause duplicates if the order of gathered objects was not the same, and it would cause clear to clear 10 at a time when using folder.

dapf27 commented 1 week ago

Thanks for the update but I still get duplicates and with Clear-O365PersonalContact it deletes still only 10 contacts per run.

PrzemyslawKlys commented 1 week ago

Which version are you using? Can you provide logs?

dapf27 commented 1 week ago
ModuleType    Version    PreRelease   Name
----------    -------    ----------   ----
Script        1.0.2                   O365Synchronizer

What logs do you need?

First run: image Second run: image

Clear with only 10: image

timecop77 commented 2 days ago

first of all: Thank you for your great work !

I have the same issue when I work with Folders. First Sync (e.g. 300 contacts) works well. Second sync -> the script says the User have only 10 contacts and will process 300 contacts. So I have 290 duplicates.

But all work fine without separate Folders (standard contact folder)

I use the lastest Version 1.0.2

PrzemyslawKlys commented 1 day ago

The problem comes from:

Get-MgUserContactFolder -UserId $Identity -Filter "DisplayName eq '$FolderName'" -ExpandProperty Contacts -ErrorAction Stop -ALl

The ALL switch doesn't do anything. So I need to find proper way to do this.

PrzemyslawKlys commented 1 day ago

This release should fix this issue: