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

Understanding the Script #25

Closed NGBIT-de closed 3 years ago

NGBIT-de commented 3 years ago

Hello everyone,

I came across the script here. This is my first post at Github.

I have to implement it for my employer as it is written in the script and unfortunately I have some problems with understanding.

Basically it works. Many thanks to the author.

Problem no 1: In the mailboxlist I can enter an address or DIRECTORY. Both works. But I have to fill a handful of addresses. As in the example of the description with $MailboxList = @("myemail1@domain.com", "myemail2@domain.com", "myemail3@domain.com") it does not work. I tried some variants, unfortunately without success.

Problem no 2: I would like to use the optional parameters ExcludeContactsWithoutPhoneNumber and ExcludeSharedMailboxContacts In the script I set the parameters to $true. Unfortunately the script does not run anymore. I have tried some variants here as well, unfortunately without success.

It would be great if someone could help me with this. I am not the fittest in scripting and programming.

Thanks and best regards Alex

grahamr975 commented 3 years ago

Problem 1

$MailboxList = @("myemail1@domain.com", "myemail2@domain.com", "myemail3@domain.com")

This won't work from a .bat file since batch can't pass complex variables like lists to PowerShell.. You'll need to either instead pass your parameters from a PowerShell script or hard code your list in the EWSContactSync.ps1 file (see line 70). I set up an example of this for you below.

[Parameter(Mandatory=$True)]
    [String]
    $FolderName,

    [Parameter(Mandatory=$True)]
    [String[]]
    $MailboxList = @("john.doe@mycompany.com","jane.doe@mycompany.com"),

    [Parameter(Mandatory=$false)]
    [String]
    $ClientID,

Problem 2

ExcludeContactsWithoutPhoneNumber and ExcludeSharedMailboxContacts are switches. There's no need to set them to $true in the .bat file. Just include instead. See below for an example.

@echo off
cd "%~dp0EWS-Office365-Contact-Sync"

PowerShell.exe -ExecutionPolicy Bypass ^
-File "%CD%\EWSContactSync.ps1" ^
-CredentialPath "C:\Encrypted Credentials\SecureCredential.cred" ^
-FolderName "Directory Contacts" ^
-LogPath "%~dp0Logs" ^
-MailboxList DIRECTORY ^
-ExcludeContactsWithoutPhoneNumber ^
-ExcludeSharedMailboxContacts 
pause
NGBIT-de commented 3 years ago

many thanks for your feedback grahamr975

I tried it directly.

To problem 1. If I enter this in the Powershell Script EWSContactSync.ps1 and leave it in the Batch -MailboxList with an email address, it still only makes the email address from the batch file. If I leave -MailboxList completely out of the batch file, it will ask for a value.

To problem 2. I already tried this variant yesterday. If I put it in the batch file, the batch runs through and at the end of the batch it says, that the command is written wrong or not found.

Thanks a lot in advance for your help.

Translated with www.DeepL.com/Translator (free version)

grahamr975 commented 3 years ago

No problem, happy to help.

Problem 1

I see the issue now. You also need to change Mandatory=$True to Mandatory=$False on line 68 of EWSContactSync.ps1. This will allow you to not pass a parameter for $MailboxList from your batch file. See the below example.

[Parameter(Mandatory=$True)]
    [String]
    $FolderName,

    [Parameter(Mandatory=$False)]
    [String[]]
    $MailboxList = @("john.doe@mycompany.com","jane.doe@mycompany.com"),

    [Parameter(Mandatory=$false)]
    [String]
    $ClientID,

Problem 2

Hmmm.... Are you including the carrot symbols (^) in the proper places for your batch file? Be sure to double-check this. Could you reply with a screenshot of your error? Also, could you include a screenshot of your batch file (with sensitive data omitted)?

NGBIT-de commented 3 years ago

OK now I have it. Thanks a lot, Ryan. You got a star from me. Thank you so much for the work. This will make my employer super happy.

Both problems are solved.

If

[Parameter(Mandatory=$False)] [String[]] $MailboxList = @("john.doe@mycompany.com", "jane.doe@mycompany.com"),

then the switch parameters in the batch will also work. -ExcludeContactsWithoutPhoneNumber ^ -ExcludeSharedMailboxContacts ^

They also work for -MailboxList DIRECTORY ^

So I have two options, in case we would take all of them from the directory after all.

So you are welcome to close the question.

Best Regards Alex

Chudinnio commented 3 years ago

Hello everyone,

This conversation realy helps me to understand script but i have a problem. I would like to deploy script on AD Group. So i changed Mailbox Parameter code:

[Parameter(Mandatory=$False)] [String[]] $MailboxList = @(Get-ADGroupMember -Identity ExampleName -recursive | get-aduser -Properties mail | select mail),

in powersahell when i check only: Get-ADGroupMember -Identity ExampleName -recursive | get-aduser -Properties mail | select mail, i got list of users but when i use it in parameter i got mail adres in format: @{mail=name.surname@domain.com} after that i got error that email adres is in bad format. What should i do? Any sugesstions?