Solved

add-adgroupmember with a variable.

Posted on 2010-08-24
13
2,431 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A procedure for exporting installed hotfix details of remote computers using powershell
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

770 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