[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add multiple security groups to computers using PowerShell

Posted on 2014-08-13
2
Medium Priority
?
1,717 Views
Last Modified: 2014-08-13
Help with script that can add multiple member groups to computers.

My script works if I only get one member group and assign it to a variable but when I assign multiple groups to a variable it doesn't like it. Someone please feel free to school me here.

# Computer Path to my computers in ADUC
$CP = "OU=folder,DC=domain,DC=com"

# Group Path to my member groups
$GP = "OU=Groups,DC=domain,DC=com"

# Groups that I want to add to computers
$SecGroup = Get-ADGroup -Filter {Name -like 'MemGroup1' -or Name -like 'MemGroup2' -or Name -like 'MemGroup3'} -SearchBase $GP

$C = Get-ADComputer -Filter 'Name -like "ComputerName"' -Properties * -SearchBase $CP

PS > Add-ADGroupMember $SecGroup -Members $C

Add-ADGroupMember : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not supported.

Open in new window


This works however if I change my $SecGroup variable to only grab one member group. But if I add more than one group then it breaks?

Thanks
0
Comment
Question by:ryanmaves
2 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 2000 total points
ID: 40259223
In the following Syntax.. You can see the the Identity parameter doesn't accept an array of values like Members parameter..
Add-ADGroupMember -Identity <ADGroup> -Members <ADPrincipal[]>

Open in new window

You can use Foreach to loop the groups to assign membership..
$SecGroup | %{Add-ADGroupMember $_.Name -Members $C}

Open in new window

0
 

Author Closing Comment

by:ryanmaves
ID: 40259240
Perfect!

I was trying to use the ForEach-Object cmdlet but looks like I was using it incorrectly. Thanks for the tip it even works if I assign multiple computers to $C, excellent!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Let's recap what we learned from yesterday's Skyport Systems webinar.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

831 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