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

Only accounts with a mailbox are included #17

Open Juergen100 opened 4 years ago

Juergen100 commented 4 years ago

Hi Ryan, thanks for the script.

Is it possible to get all users in Office 365 with an email adress?

In your old script you used "$Users = Get-User -Filter {WindowsEmailAddress -ne $null} -ResultSize unlimited", it seems that in your new script only users with a mailbox in Exchange Online are included. We have an Exchange Hybrid environment and not all users are migrated to Exchange Online.

Thanks for response

Juergen100 commented 4 years ago

I see, it's the ExcludeSharedMailboxContacts parameter in the function Get-GALContacts.ps1. This also exclude all users without an Exchange Online mailbox and not only shared mailboxes.

grahamr975 commented 4 years ago

Juergen100,

I'll update the ExcludeSharedMailboxContacts switch and documentation to provide better clarity. Did removing the switch take care of your issue?

Juergen100 commented 4 years ago

grahamr975,

Yes, without the ExcludeSharedMailboxContacts switch I get all users, unfortunately also the shared mailboxes. I have experimented with something and found the following:

If i change in the Get-GALContacts.ps1 script the code to

# Import Global Address List into Powershell from Office 365 Exchange as an array
$ContactList = Get-User -Filter {WindowsEmailAddress -ne $null} -ResultSize unlimited | Where-Object {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'MailUser'} | Select-Object DisplayName,FirstName,LastName,Title,Company,Department,WindowsEmailAddress,Phone,MobilePhone

and start the batch without the ExcludeSharedMailboxContacts switch it works and I get all users.

If i change in the Get-GALContacts.ps1 script the code to

# Import Global Address List into Powershell from Office 365 Exchange as an array
$ContactList = Get-User -Filter {WindowsEmailAddress -ne $null} -ResultSize unlimited  | Select-Object DisplayName,FirstName,LastName,Title,Company,Department,WindowsEmailAddress,Phone,MobilePhone

# If the ExcludeSharedMailboxContacts switch is enabled, exclude contacts that are a shared mailbox
if ($ExcludeSharedMailboxContacts) {
$ContactList = $ContactList  | Where-Object {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'MailUser'} 
}

and start the batch with the ExcludeSharedMailboxContacts switch it dosen't work and I get no users. I have no clue why.

Thanks Juergen

grahamr975 commented 4 years ago

In your second example, 'Select-Object' on line 2 removes the 'RecipientTypeDetails' property from the ContactList array. So, when you check for 'UserMailbox' or 'MailUser' later, nothing is returned. Could you try the below code?

# Import Global Address List into Powershell from Office 365 Exchange as an array
$ContactList = Get-User -Filter {WindowsEmailAddress -ne $null} -ResultSize unlimited

# If the ExcludeSharedMailboxContacts switch is enabled, exclude contacts that are a shared mailbox
if ($ExcludeSharedMailboxContacts) {
$ContactList = $ContactList | Where-Object {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'MailUser'}
}

$ContactList = $ContactList | Select-Object DisplayName,FirstName,LastName,Title,Company,Department,WindowsEmailAddress,Phone,MobilePhone
Juergen100 commented 4 years ago

I tried your code, now I get users but but unfortunately again only those with mailboxes. It seems that the filter {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'MailUser'} is not considered. The same with the filter {$_.RecipientTypeDetails -ne 'SharedMailbox'}

grahamr975 commented 4 years ago

Didn't you say that your first example worked fine for you? My above code should be functionally identical to that...

# Import Global Address List into Powershell from Office 365 Exchange as an array
$ContactList = Get-User -Filter {WindowsEmailAddress -ne $null} -ResultSize unlimited | Where-Object {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'MailUser'} | Select-Object DisplayName,FirstName,LastName,Title,Company,Department,WindowsEmailAddress,Phone,MobilePhone

Can you try to it without "-Filter {WindowsEmailAddress -ne $null}"? I believe that this excludes non-mailboxes. See below.

# Import Global Address List into Powershell from Office 365 Exchange as an array
$ContactList = Get-User -ResultSize unlimited

# If the ExcludeSharedMailboxContacts switch is enabled, exclude contacts that are a shared mailbox
if ($ExcludeSharedMailboxContacts) {
$ContactList = $ContactList | Where-Object {$_.RecipientTypeDetails -eq 'UserMailbox' -or $_.RecipientTypeDetails -eq 'MailUser'}
}

$ContactList = $ContactList | Select-Object DisplayName,FirstName,LastName,Title,Company,Department,WindowsEmailAddress,Phone,MobilePhone

If this still does not work, you can try without "-Filter {WindowsEmailAddress -ne $null}" AND using the "{$_.RecipientTypeDetails -ne 'SharedMailbox'} filter."

# Import Global Address List into Powershell from Office 365 Exchange as an array
$ContactList = Get-User -ResultSize unlimited

# If the ExcludeSharedMailboxContacts switch is enabled, exclude contacts that are a shared mailbox
if ($ExcludeSharedMailboxContacts) {
$ContactList = $ContactList | Where-Object {$_.RecipientTypeDetails -ne 'SharedMailbox'}
}

$ContactList = $ContactList | Select-Object DisplayName,FirstName,LastName,Title,Company,Department,WindowsEmailAddress,Phone,MobilePhone
Juergen100 commented 4 years ago

During my last test I have made a programming error. With this code the switch works as expected:

# Import Global Address List into Powershell from Office 365 Exchange as an array
$ContactList = Get-User -Filter {WindowsEmailAddress -ne $null} -ResultSize unlimited 

# If the ExcludeSharedMailboxContacts switch is enabled, exclude contacts that are a shared mailbox
if ($ExcludeSharedMailboxContacts) {
    $ContactList = $ContactList | Where-Object {$_.RecipientTypeDetails -ne 'SharedMailbox'}
} 
$ContactList = $ContactList | Select-Object DisplayName,FirstName,LastName,Title,Company,Department,WindowsEmailAddress,Phone,MobilePhone
grahamr975 commented 4 years ago

When I have time, I'll test this myself and hopefully incorporate this fix into the next version. Thanks