Creating computers and adding them to list of groups in powershell

Good morning,

$OU= 'OU=Computers Win7,OU=something,OU=office,DC=net,DC=company,DC=co,DC=uk'
$computers = get-content 'C:\Powershell Projects\addcomputers\DB.txt'
$Groups = get-content 'C:\Powershell Projects\addcomputers\DBgroup.txt'

 ForEach ($Computer in $Computers) 
 { 
 New-ADComputer -Name $computer -Path $OU -Enabled $true
 }
Foreach ($Group in $groups) {

 $Computername = $computer.name + '$'
 
 Add-ADGroupMember -Identity $group -Members $Computername
 }

Open in new window



So, it creates the machine and that's all fine, the issue is adding the computer to a list of groups which I have specified in the text file.

Example below

Domain users
Enterprise users
group 1
group 2



Now I get the error as follows

Add-ADGroupMember : Cannot find an object with identity: '$' under: 'DC=net,DC=CompanyDC=co,DC=uk'.
At line:13 char:2
+  Add-ADGroupMember -Identity $group -Members $Computername
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: ($:ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
    + FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember


Any ideas?
LVL 16
Alex Green3rd Line Server SupportAsked:
Who is Participating?
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.

oBdACommented:
Because in your second loop, "$Computer.name" just doesn't exist.
What you probably want is this:
$OU = 'OU=Computers Win7,OU=something,OU=office,DC=net,DC=company,DC=co,DC=uk'
$computers = get-content 'C:\Powershell Projects\addcomputers\DB.txt'
$Groups = get-content 'C:\Powershell Projects\addcomputers\DBgroup.txt'

ForEach ($Computer in $Computers) { 
	New-ADComputer -Name $Computer -Path $OU -Enabled $true
	Foreach ($Group in $groups) {
		Add-ADGroupMember -Identity $group -Members "$($Computer)$"
	}
}

Open in new window

0

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 Green3rd Line Server SupportAuthor Commented:
YOU'RE SO GOD DAMN GOOD!!!!!


Thanks mate, as always very much appreciated.
0
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
Powershell

From novice to tech pro — start learning today.

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.