Solved

Help tidying up Powershell script mulitple variables

Posted on 2013-01-14
2
880 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…

803 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