1
votes

I am trying to add a list of users to an AD Security group using the below Powershell script and I am getting the following error. Add-ADGroupMember : Cannot find an object with identity:

Can anoybody advise on what I am doing wrong. The user names do exist in AD

Import-Module ActiveDirectory 
$comps=Get-Content names.txt 

foreach ($comp in $comps)
{$dns=get-aduser $comp
$b=$dns.distinguishedname
Add-ADGroupMember -Identity  $b GIT_GS_AMU_Windows7_Object 
}
4

4 Answers

0
votes

I haven't used the AD module for a while, but Identity is the group, not the member. Also, are you adding computers or users? $comp indicates computer, while get-aduser indicates user. :S

The TechNet article says that -Members should allow an array of principals which could be DN, objects and even samaccountnames, so you could try:

Import-Module ActiveDirectory 
$comps=Get-Content names.txt 

Add-ADGroupMember -Identity "GIT_GS_AMU_Windows7_Object" -Members $comps
0
votes

Try this:

$grp = 'GIT_GS_AMU_Windows7_Object '

Import-Module ActiveDirectory 
$comps=Get-Content names.txt 

$grpDN = (get-adgroup $grp).distinguishedname

foreach ($comp in $comps)
{$dns=get-aduser $comp
$b=$dns.distinguishedname
Add-ADGroupMember -Identity  $grpDN -member $dns 
}
0
votes

If you want to add users to a group using alternate credentials you can use this:

Import-module ActiveDirectory 
$cred = Get-Credential
Get-Content ".\users.txt" | % { 
Add-ADGroupMember -Credential $cred -Identity Test_Group -Member $_
} 
0
votes

You have your arguments flipped for Add-ADGroupMember. -Identity is the group, not the user(s).

Try this:

Import-Module ActiveDirectory 
$comps=Get-Content names.txt 

foreach ($comp in $comps) {
  Get-ADUser $comp | Add-ADGroupMember GIT_GS_AMU_Windows7_Object -Members $_
}

However, if you have a lot of users, it would be more efficient to add them to the group at once rather than individually. So something like

Import-Module ActiveDirectory 
$comps=Get-Content names.txt 
$users = @()

$Users = foreach ($comp in $comps) {
  Get-ADUser $comp
}
Add-ADGroupMember GIT_GS_AMU_Windows7_Object -Members $users