Object issues with Powershell

Ok so,

I've got this code, it pulls the CSV file which is a file named the same as the group, the file contains the objects within the group. in this case, computer objects.

$groups = get-content "C:\JLBR\JLBR.txt"
Foreach ($group in $Groups) {Import-csv "C:\JLBR\$($group).csv" | ForEach-Object {Add-ADGroupMember -Identity $Group -Members $_.name}}

Open in new window


Now, this code kinda works, well, it doesn't I get the error

Add-ADGroupMember : Cannot find an object with identity: 'CS-Computername' under: 'DC=net,DC=Domainaccept,DC=co,DC=uk'.
At line:2 char:83
+ ... ForEach-Object {Add-ADGroupMember -Identity $Group -Members $_.name}}
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ('CS-Computername:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember


I think this is because I'm trying to add a computer object, any ideas?

Thanks
Alex
LVL 15
Alex Green3rd Line Server SupportAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
To add a computer account this way, you need to use the cmputer's samAccountName, which is its name with a trailing '$' (and even though syntactically, the backtick isn't required here, it helps indicating that a literal '$' is meant):
$groups = get-content "C:\JLBR\JLBR.txt"
Foreach ($group in $Groups) {Import-csv "C:\JLBR\$($group).csv" | ForEach-Object {Add-ADGroupMember -Identity $Group -Members "$($_.name)`$"}}

Open in new window

0
 
Alex Green3rd Line Server SupportAuthor Commented:
My hero! You have to give me a little credit though, with your help I've gotten better ;-)

Thanks mate :D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.