Solved

add-adgroupmember with a variable.

Posted on 2010-08-24
13
2,506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upper Case in powershell Paramter 5 28
Calling a function 4 24
Exchange 2013 Message Tracking 3 38
NTDS CN=Infrastructure FSMORoleOwner 5 16
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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