Solved

Help tidying up Powershell script mulitple variables

Posted on 2013-01-14
2
876 Views
Last Modified: 2013-01-15
I have created this script which adds the computers in multiple OUs to a security group:

$OU1 "DN of OU 1"
$OU2 "DN of OU2"
$OU3 "DN of OU3"

$Group "DN of group"
Get-ADComputer –SearchBase $OU1 –SearchScope OneLevel –LDAPFilter “(!memberOf=$Group)” | ForEach-Object {Add-ADPrincipalGroupMembership –Identity $_ –MemberOf $Group}
Get-ADComputer –SearchBase $OU2 –SearchScope OneLevel –LDAPFilter “(!memberOf=$Group)” | ForEach-Object {Add-ADPrincipalGroupMembership –Identity $_ –MemberOf $Group}
Get-ADComputer –SearchBase $OU3 –SearchScope OneLevel –LDAPFilter “(!memberOf=$Group)” | ForEach-Object {Add-ADPrincipalGroupMembership –Identity $_ –MemberOf $Group}

Is there a way to shorten this so I can specify each OU in one line of code rather than a seperate script for each OU?
0
Comment
Question by:infradatel
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38774167
$Group = "DN of group"
"DN of OU 1","DN of OU2", "DN of OU3" | %{
Get-ADComputer –SearchBase $_ –SearchScope OneLevel –LDAPFilter “(!memberOf=$Group)” | ForEach-Object {Add-ADPrincipalGroupMembership –Identity $_ –MemberOf $Group}
}

Open in new window

0
 

Author Closing Comment

by:infradatel
ID: 38777463
Works great thanks
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now