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

Working on using distro lists/group to apply #62

Open Tom-Rudd opened 1 year ago

Tom-Rudd commented 1 year ago

Hello all, I am trying to make it so I can query distribution lists to apply the sync to select users. So far, I have the command that seems to be pooping out just the email addresses as I would want.:

[Parameter(Mandatory=$False)]
[String[]]
$MailboxList = @(Get-DistributionGroupMember "DISTRONAME" | Get-Mailbox | select 
PrimarySMTPAddress).primarysmtpaddress,

When I run the command in exchange online PowerShell, I get a list back of all the email addresses. However, when I insert it into the parameter statement I get:

cmdlet EWSContactSync.ps1 at command pipeline position 1
Supply values for the following parameters:
MailboxList[0]:
C:\Scripts\EWSContactSync\EWSContactSync.ps1 : Cannot bind argument to parameter 'MailboxList' because it is an empty
array.
+ CategoryInfo          : InvalidData: (:) [EWSContactSync.ps1], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyArrayNotAllowed,EWSContactSync.ps1

What am I missing here, I thought that would be exactly what it needs. Do I need to insert that command into another location in the scripts?

Tom-Rudd commented 1 year ago

I solved this by adding this into the script, for anyone who needs it. This works when you have an onsite AD that has the correct emails of all the users you want to sync, and you have the powershell AD tools installed.

 # If 'DIRECTORY' is used for $MailboxList, fetch all Mailboxes from the administrator account's Office 365 directory
 if ($MailboxList -eq "DIRECTORY") {
$MailboxList = Get-Mailboxes -ConnectionUri https://outlook.office365.com/powershell-liveid/ -CertificatePath 
$CertificatePath -CertificatePassword $CertificatePassword -ExchangeOrg $ExchangeOrg -ClientID $ClientID
}
if ($MailboxList -eq "GROUP") {
$MailboxList = (Get-ADGroupMember "YOUR USER ACCOUNT" -recursive | get-aduser | select 
UserPrincipalName).userprincipalname
rypto commented 1 year ago

So say I want the script to apply to a security group called "Employees" instead of everyone using the default DIRECTORY parameter. Where do I put your line?