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?

LVL 18
Alex GreenProject Systems EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alex GreenProject Systems EngineerAuthor Commented:
My hero! You have to give me a little credit though, with your help I've gotten better ;-)

Thanks mate :D
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.