Solved

add-adgroupmember with a variable.

Posted on 2010-08-24
13
2,379 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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…
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 to another domain controller. Log onto the new domain controller with a user account t…

705 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

18 Experts available now in Live!

Get 1:1 Help Now