1

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 回答 4

0

如果您想使用备用凭据将用户添加到组中,您可以使用以下命令:

Import-module ActiveDirectory 
$cred = Get-Credential
Get-Content ".\users.txt" | % { 
Add-ADGroupMember -Credential $cred -Identity Test_Group -Member $_
} 
于 2014-03-07T12:51:58.420 回答
0

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
于 2013-11-08T17:30:34.073 回答
0

我有一段时间没有使用AD模块了,但是Identity是组,而不是成员。另外,您是在添加计算机还是用户?$comp表示计算机,而get-aduser表示用户。:S

TechNet 文章说应该允许一组主体,这些-Members主体可以是 DN、对象甚至 samaccountnames,因此您可以尝试:

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

Add-ADGroupMember -Identity "GIT_GS_AMU_Windows7_Object" -Members $comps
于 2013-11-08T17:33:29.043 回答
0

尝试这个:

$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 
}
于 2013-11-08T17:36:09.287 回答