Link to home
Start Free TrialLog in
Avatar of Mohammed Hamada
Mohammed HamadaFlag for Portugal

asked on

Adding members to Exchange Distribution Group from a file based on certain criteria

Dear all,

I would like to know how to add a user to an exchange distribution group by matching the user's from a text file. My text file is as following

Group,Email

My current powershell is as following

$Users = Import-Csv "C:\Scripts\Users.csv"
foreach ($User in $Users){
$member = $user.email
group =$user.group

if($group -like "CertainExchangeGroup"){
Add-DistributionGroupMember -Identity  "CertainGroup@domain.com" -Member $member{
}
Write-Host $member has been added to $group -ForegroundColor Green}
}

Open in new window


At the moment this doesn't work and gives the following error
A positional parameter cannot be found that accepts argument '
'.
    + CategoryInfo          : InvalidArgument: (:) [Add-DistributionGroupMember], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Add-DistributionGroupMember
    + PSComputerName      

I would appreciate any help to fix this
Thank you
Avatar of oBdA
oBdA

That's because of the empty scriptblock directly after "$member".
Indent properly; it's more than eye candy, it helps you identify how deeply you're nested.
$Users = Import-Csv "C:\Scripts\Users.csv"
ForEach ($User in $Users) {
	$member = $user.email
	$group = $user.group
	If ($group -like "CertainExchangeGroup") {
		Add-DistributionGroupMember -Identity "CertainGroup@domain.com" -Member $member
		Write-Host $member has been added to $group -ForegroundColor Green
	}
}

Open in new window

Avatar of Mohammed Hamada

ASKER

Thanks a lot oBdA that worked as always. I just have another tiny question maybe you could aid me with it.

I want the group to be dynamic not something that I identify there. Is it possible to do so ?
for example
$Users = Import-Csv "C:\Scripts\Users.csv"
ForEach ($User in $Users) {
	$member = $user.email
	$group = $user.group
	If ($group -like "get-distributiongroup | select name") {
		Add-DistributionGroupMember -Identity "$group" -Member $member
		Write-Host $member has been added to $group -ForegroundColor Green
	}
}

Open in new window


Do you think that would work? matching the existing active directory groups with the one in the csv list and add the users in csv to that particular Exchange Distribution group?

Thanks a lot
Sorry, I can't quite follow what it is you want to do.
You have a csv as input anyway, so why not just adjust the csv to contain only the groups you want to process?
The groups are the same in Active Directory and in the CSV file but Distribution groups are empty and I want to add members according to the csv file.

The csv file is the one that's up to date.
So - why the group name check at all? Why not just go through every row and add the user to the respective group?
Because the group in CSV doesn't entirely matches the one in AD. The name could be wrong so I want to double check if the group exists and can be matched then add the user.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you as always