This may be the wrong approach, but I have used the last couple of days experimenting with the foreach
in PowerShell (I use Ver. 5 of PowerShell).
What I am looking for is a way to add a list of computers that I already have into a list of AD groups that I already have. So I used Get-Content
for importing the 2 .txt files, and I also learned that AD groups in PowerShell uses -Identity
instead of name I don't know the reason for that decision. But nevertheless I came up with this:
$Apps = Get-Content C:\Scripts\Apps.txt
$Computers = Get-Content C:\Scripts\Computers.txt
foreach ($App in $Apps) {
Add-ADGroupMember $Apps -Identity $Computers
}
My problem is that it works of I only have 1 AD group in the Apps.txt
file. If I add more groups PowerShell goes all red on me, and then my computer starts crying.
In Computers.txt
I have listed the computer accounts with a $
at the end, and they are on seperate lines, like this:
PC1$
PC2$
In Apps.txt
the AD groups are on seperated lines without any commas or semmicolons or anything.
Change $Apps
to $App
in the line Add-ADGroupMember $Apps -Identity $Computers
and also the -Identity
parameter is the AD Group name. You also will need to use the -Members
parameter for the users. E.g.
Add-ADGroupMember -Identity $App -Members $Computers