Solved

add-adgroupmember with a variable.

Posted on 2010-08-24
13
2,484 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

685 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