Solved

add-adgroupmember with a variable.

Posted on 2010-08-24
13
2,401 Views
Last Modified: 2012-05-10
Hello,

I`m building script one of the things i have to do is the following:
$grpmembers = "user1,user2,user3"
$dataadgrp = "grdatatest"
add-adgroupmember -identity $dataadgrp -members $grpmembers
 
but it doenst work.
It only works when i do this:
add-adgroupmember -identity $dataadgrp -members user1,user2,user3

this is the error that i get:

Add-ADGroupMember : Cannot find an object with identity: 'user1,user2,user'

Thanks

Regards

Hans
0
Comment
Question by:Hans de Jongh
  • 8
  • 5
13 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 33514259
$grpmembers = "user1,user2,user3" should be:
$grpmembers = "user1","user2","user3"
0
 

Author Comment

by:Hans de Jongh
ID: 33514305
thanks, but that gives the same error message..

0
 
LVL 13

Expert Comment

by:soostibi
ID: 33514532
It can not give the same error message. Could you copy it here?
In my test environment it works perfectly.

Does this run?

$grpmembers = "user1","user2","user3"
$grpmembers | Get-ADUser
0
 

Author Comment

by:Hans de Jongh
ID: 33514750
PS D:\Scripts> $test3 = "user.name1","user.name2"
PS D:\Scripts> Add-ADGroupMember -Identity $dataadgrp -members $test3
Add-ADGroupMember : Cannot find an object with identity: 'user.name1 user.name2' under: 'DC=domain,DC=local
'.
At line:1 char:18
+ Add-ADGroupMember <<<<  -Identity $dataadgrp -members $test3
    + CategoryInfo          : ObjectNotFound: (user.name1 user.name2:ADPrincipal) [Add-ADGroupMember], ADIdentityN
   otFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.
   AddADGroupMember


PS D:\Scripts> $test3 = "user.name1, user.name2"
PS D:\Scripts> Add-ADGroupMember -Identity $dataadgrp -members $test3
Add-ADGroupMember : Cannot find an object with identity: 'user.name1, user.name2' under: 'DC=domain,DC=local
'.
At line:1 char:18
+ Add-ADGroupMember <<<<  -Identity $dataadgrp -members $test3
    + CategoryInfo          : ObjectNotFound: (user.name1, user.name2:ADPrincipal) [Add-ADGroupMember], ADIdentityN
   otFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.
   AddADGroupMember


0
 
LVL 13

Expert Comment

by:soostibi
ID: 33515208
I do not understand your first error message. It says: Cannot find an object with identity: 'user.name1 user.name2' So it does not use the array of usersname what should be in variable $test3.
In my environment if I use nonexistent users I receive the following error
$grpmembers = "xx", "yy"
$dataadgrp = "csoport"
add-adgroupmember -identity $dataadgrp -members $grpmembers
Add-ADGroupMember : Cannot find an object with identity: 'xx' under: 'DC=r2,DC=dom'.
At line:3 char:18
+ add-adgroupmember <<<<  -identity $dataadgrp -members $grpmembers
    + CategoryInfo          : ObjectNotFound: (xx:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
 
What does this give
$grpmembers | Get-ADUser
0
 

Author Comment

by:Hans de Jongh
ID: 33515242
Add-ADGroupMember : Cannot find an object with identity: 'user.name1 user.name2' under: 'DC=domain,DC=local
'.
At line:1 char:18
+ Add-ADGroupMember <<<<  -Identity $dataadgrp -members $test3
    + CategoryInfo          : ObjectNotFound: (user.name1 user.name2:ADPrincipal) [Add-ADGroupMember], ADIdentityN
   otFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.
   AddADGroupMember
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:Hans de Jongh
ID: 33515688
maybe it is because my users arent in the default location?

0
 

Author Comment

by:Hans de Jongh
ID: 33515852
ive tested it in another domain. same problem...
0
 

Author Comment

by:Hans de Jongh
ID: 33515918
$test1 = "hans, remco"
$test2 = Get-ADUser -filter *
add-adgroupmember grtest -member $test1  <- doenst work
add-adgroupmember grtest -member $test2 <- does work

0
 
LVL 13

Expert Comment

by:soostibi
ID: 33515946
But what does this give?

$grpmembers | Get-ADUser

If this gives an error, then put only one username into $grpmembers first:

$grpmembers = "onlyoneuser"
$grpmembers | Get-ADUser

If this is OK, then try with anotherone:

$grpmembers = "anotheruser"
$grpmembers | Get-ADUser

If this is also OK, then:

$grpmembers = "onlyoneuser", "anotheruser"
$grpmembers | Get-ADUser

If this is OK, then the original script must run successfully!
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33515976
Sorry, cross-posts.
$test1 = "hans, remco" will never run, if hans and remco are two users.


0
 

Author Closing Comment

by:Hans de Jongh
ID: 33516221
Thanks Master!!
0
 

Author Comment

by:Hans de Jongh
ID: 33516230
dont know what i did wrong but it works now!!

Import-CSV -Delimiter (";") $CSVFiledatagrps | ForEach-Object -Process {
$group = $_.Group
$group = $group.replace(" ","_")
$dataadgrp = "grDir-"+"$companyname"+"_$group"
$directory = $_.Directory
$grpmembers = $_.members
$grpmembers = $grpmembers.replace(", ",'","')
$grpmembers
$dataadgrp
$grpmembers = '"'+$grpmembers+'"'
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now