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
99 stars 21 forks source link

Howto specify more than one mailbox to sync from command line ? #88

Closed buzzzo closed 5 days ago

buzzzo commented 1 year ago

As per subject I can't figure out the exact syntax of -mailboxlist when using more than one entries. Every time i'm trying to specify in some format the script gives error.

Thx

grahamr975 commented 1 year ago

You can't define a PowerShell list in a batch file. The easiest way to do this is to edit the EWSContactSync.ps1 file. Change the Mandatory value of the MailboxList parameter from $True to $False. Then, write in your custom addresses in the following format: @(email1, email2, emai3, etc...). At this point, you should remove the MailboxList argument from your batch file since you already defined the emails in EWSContactSync.ps1. See below for an example of what EWSContactSync.ps1 should look like. Hopefully this helps.

[Parameter(Mandatory=$False)]
    [String[]]
    $MailboxList = @(john.doe1@mycompany.com, john.doe2@mycompany.com, , john.doe3@mycompany.com)
buzzzo commented 1 year ago

Thx for response.

I've ended up by modifying the ps1 file but just let get the email addresses list from an external file and then populate the array via foreach loop.

Thx for help

BerryC commented 1 year ago

Hi Buzzzo,

Do you have an example of the modified ps1 file?

philipfredberg commented 1 year ago

@buzzzo do you have an example of the file/foreach loop that you can share?

buzzzo commented 1 year ago

Hi

I've manage to create a txt file with all the mailbox name to sync and calling it in the EWSContactSync script to populate an array.

So: 1) an external script create a file with ALL the username@domain i would like to sync. This is the script: (Group_To_Sync is the nome of the AD group holding ALL the user i would like to create the gal, so basically of user in this group will be written in mailboxsync.txt file)

$file = "c:\o365\gal-exo\mailboxtosync.txt"

if (Test-Path $file ) { Remove-Item $file }

$users = Get-ADGroupMember Group_ToSync | %{get-aduser $.SamAccountName | select userPrincipalName } | Select-Object -ExpandProperty userPrincipalName |Out-File -filepath $file

2) I've add this on ewcontactsync script:

if ( Test-Path -Path "c:\o365\gal-exo\mailboxtosync.txt" -PathType Leaf ) { $MailboxList = Get-Content -Path @("C:\o365\gal-exo\mailboxtosync.txt") }

Of course you need to run script1 BEFORE ewcontactsync script in order to create the mailboxtosync.txt script

Hope this can help