How do I create a Powershell command to link the attribute 'City' in AD to a DL in Exchange and then auto add Users created for this location?

Darrin Crawford
Darrin Crawford used Ask the Experts™
on
I need help with a Powershell command.

So we have several of these working already working with our AD to Office 365 - Exchange environment.

It is a powershell command that looks to an Attribute in AD - called City - of which picks up in this case florida.

This in turn adds the User to a DL - called AllFloridaUsers in our cloud Exchange environment in Office 365.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AlexSenior Infrastructure Analyst

Commented:
Get-aduser -filter "CIty -like 'Florida'" -searchbase "Enter DN of searchbase" -properties City  | Add-adgroupmember -groupname AllFloridaUsers


I'd put the search base in so you don't affect accounts you shouldn't do.
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
If your groups are created directly in O365, here's an updated variant:

Get-User -Filter {City -eq "Sofia"} | % { Add-DistributionGroupMember AllFloridaUsers -Member $_.UserPrincipalName  }

Open in new window

Author

Commented:
Thanks guys, unfortunately getting errors with both - for first one, not recognizing Ad-AddGroupMember....  The second one is giving errors around 'Member' , saying it is null
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Well, did you change the City name, I used "Sofia" to test it with my users :)

Author

Commented:
How do I edit this on Exchange through Powershell?


((((((((((((((((((((((RecipientTypeDetails -eq 'UserMailbox') -and (RecipientTypeDetails -eq 'UserMailbox'))) -and (City -eq 'Florida'))) -and (-not(CustomAttribute1 -eq 'Contractor')))) -and (-not(Name -like 'SystemMailbox{*')))) -and (-not(Name -like 'CAS_{*')))) -and (-not(RecipientTypeDetailsValue -eq 'MailboxPlan')))) -and (-not(RecipientTypeDetailsValue -eq 'DiscoveryMailbox')))) -and (-not(RecipientTypeDetailsValue -eq 'PublicFolderMailbox')))) -and (-not(RecipientTypeDetailsValue -eq 'ArbitrationMailbox')))) -and (-not(RecipientTypeDetailsValue -eq 'AuditLogMailbox')))) -and (-not(Name -like 'SystemMailbox{*')) -and (-not(Name -like 'CAS_{*')) -and (-not(RecipientTypeDetailsValue -eq 'MailboxPlan')) -and (-not(RecipientTypeDetailsValue -eq 'DiscoveryMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'PublicFolderMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'ArbitrationMailbox')) -and (-not(RecipientTypeDetailsValue -eq 'AuditLogMailbox')

Author

Commented:
Yeah Vasil - I changed the name
Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
The cmdlet runs just fine for me:

Get-User -Filter {City -eq "Sofia"} | % { Add-DistributionGroupMember DG -Member $_.UserPrincipalName -WhatIf }
What if: Adding distribution group member "HuKu@michev.onmicrosoft.com" on distribution group "DG".
What if: Adding distribution group member "pesho@michev.info" on distribution group "DG".

Open in new window


Do you get any results for the filter? Can you shown an example of what the City attribute looks like for said users?

If you want to use a Dynamic DG, you need this query:

Set-DynamicDistributionGroup DDG -RecipientFilter {((RecipientTypeDetails -eq 'UserMailbox') -and (City -eq 'Florida') -and (-not(CustomAttribute1 -eq 'Contractor')))}

Open in new window


The rest will be added automatically.

Author

Commented:
Thanks - that is exactly what i was looking for :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial