Avatar of thakurdinesh
thakurdineshFlag for United States of America

asked on 

PowerShell script to add AD user in Distribution List using AD attribute

IM trying to find a PowerShell script that will populate active directory Distribtuion lists by using an AD Attribute of any user.

For example, a PoewrShell script that retrieves a selected Active Directory attribute  for all users (An attached file where fmCostCenter attribute have value of 200, 212 and 218 ), then parses this or interprets in some way, then based on this adds each user with the matching attribute value add to a distribution group. Before adding the user to Distribution Group make a check if the user is already present or not.

Script can be extended to remove the users from Distribution List those do not match this attribute value anymore. Because users keeps moving between different cost centers and do not want to be part of Distribution List.

Appreciate your help..
Thanks




Adsi.png
Powershell

Avatar of undefined
Last Comment
thakurdinesh
Avatar of KenMcF
KenMcF
Flag of United States of America image

You can do this using the Quest AD cmdlets.
http://www.quest.com/powershell/activeroles-server.aspx
 Depending on what your group names are you will need to modify the script. This script uses the group names of
"Cost_Center_Group_200"
"Cost_Center_Group_212"
"Cost_Center_Group_218"


$CostCenters = "200", "212", "218"
foreach ($CostCenter in $CostCenters){
#Add Users
get-qaduser -ldapfilter "(fmCostCenter=$CostCenter)" -notmemberof "Cost_Center_Group_$CostCenter" | Add-QADGroupMember "Cost_Center_Group_$CostCenter"
#Remove Users
Get-QADGroupMember "Cost_Center_Group_$CostCenter" -properties fmCostCenter | Where {$_.fmCostCenter -ne "$CostCenter"} | foreach{Remove-QADGroupMember "Cost_Center_Group_$CostCenter" $_}
}

Open in new window

Avatar of KenMcF
KenMcF
Flag of United States of America image

Here is another one that will be a little more efficient if you have large group membership


$CostCenters = "200", "212", "218"
foreach ($CostCenter in $CostCenters){
#Add Users
get-qaduser -ldapfilter "(fmCostCenter=$CostCenter)" -notmemberof "Cost_Center_Group_$CostCenter" | Add-QADGroupMember "Cost_Center_Group_$CostCenter"
#Remove Users
Get-QADGroupMember "Cost_Center_Group_$CostCenter" -LdapFilter "(!(fmCostCenter=$CostCenter))" | foreach{Remove-QADGroupMember "Cost_Center_Group_$CostCenter" $_}
}

Open in new window

Avatar of thakurdinesh
thakurdinesh
Flag of United States of America image

ASKER

Ken, I proposed this solution using Quest AD but rejected.. So I cannot use any quest cmdlet because of some scheduled jobs..

So can only use windows cmdlets.. I have similar VB script but that solution is also rejected.

Thanks
Avatar of KenMcF
KenMcF
Flag of United States of America image

What version of OS and Exchange are you running? Do you have Windows 7 or Server 2008R2?
Avatar of thakurdinesh
thakurdinesh
Flag of United States of America image

ASKER

Exchange 2007 and Server 2008 SP2 (not R2)
Avatar of thakurdinesh
thakurdinesh
Flag of United States of America image

ASKER

Between, we need to add these users from different cost centers to just One group.

The purpose fo this group is to restrict them sending emails using transport rule..

Thanks
ASKER CERTIFIED SOLUTION
Avatar of KenMcF
KenMcF
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of RobSampson
RobSampson
Flag of Australia image

@KenMcF, sorry to hijack this thread a bit, but could I possibly get you to have a look at
https://www.experts-exchange.com/questions/27362825/Create-a-dynamic-distribution-group-using-get-date-on-whencreated.html
and offer your opinion as to whether the task is possible in Powershell?  Shouldn't take long....

Thanks, again...sorry....

Rob.
Avatar of thakurdinesh
thakurdinesh
Flag of United States of America image

ASKER

Thanks very much KenMcF!

Line no 8 I was not selecting the sAMAccountName...

Thanks again for your help.

Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo