How to link GPO to list of OUs

Over the next several, I will be creating GPOs which need to link to ten OUs. It’s always the same ten OUs. I’m hoping for something that I run, and it asks for the GPO name. Once the GPO name is entered, it makes a link for the GPO to each of the OUs in the list of ten.

It would seem that PS is the proper way to do this quickly, but it’s beyond my knowledge. I’ve built all kinds of stuff in Batch, but my PS chops are pretty weak.

Your help is appreciated.

—K

Kel Koop,
Welcome to the forum. :wave:t3:

What exactly is your question?

Please keep in mind … This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

We actually expect you to make an own attempt at the first place to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

Thanks, Olaf. That’s a good point, I did not mean to come across asking for someone to write this for me.
I was hoping that there might already be something similar someone would know about which I could modify.
I will do some research and see what I can come up with.
Perhaps this place could review what I write before I execute so I don’t jack up our AD/GPO stuff.

Thanks

—K

Please have a test environment for testing! :man_shrugging:t3:

I am asking for test environ.
In the meantime, here’s what I came up with:

param(
	[Parameter(Mandatory)]
	[String]$GPOName,
	[Switch]$display = $true
)

$OUs = "Canton","Centennial","Chicago","Dayton"

 $OUs | ForEach-Object {  
    $TgT="OU=users," + "OU=$_," + "DC=ourdomain,DC=local"
	New-GPLink -Name $GPOName -Target $TgT
 }

I tested this with

Write-Output "New-GPLink -Name $GPOName to -Target $TgT"

in place of the actual New-GPLink command, and it wrote out the parameters correctly.

PS Y:\CSR> powershell -executionpolicy bypass -file SgPMLinks.ps1

cmdlet SgPMLinks.ps1 at command pipeline position 1
Supply values for the following parameters:
GPOName: SgPM:DesignPower
New-GPLink -Name SgPM:DesignPower to -Target OU=users,OU=Canton,DC=ourdomain,DC=local
New-GPLink -Name SgPM:DesignPower to -Target OU=users,OU=Centennial,DC=ourdomain,DC=local
New-GPLink -Name SgPM:DesignPower to -Target OU=users,OU=Chicago,DC=ourdomain,DC=local
New-GPLink -Name SgPM:DesignPower to -Target OU=users,OU=Dayton,DC=ourdomain,DC=local

Thoughts, anyone?

—K

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Instead of an extra line of code you could use the common parameter -WhatIf on the actual command. :point_up:t3:

Great. So you’re done. :+1:t3:

I’d recommend using a little more verbose variable names like $OUList or $OUArray instead of $OUs. And $TargetOU instead of $TgT. If someone else reads this code - an in a few weeks or months are you someone else as well - it could be harder to understand than necessary. :smirk:

And please do not post images of code, error messages or console output. Instead post the plain text and format it as code.

Thanks in advance.

Appreciate the quick reply.

I hovered over the </> symbol expecting that might be a code formatter, (i’ve posted a lot on the Raspberry Pi Forum, so I understand about posting code) but it just said “Preformatted text ctrl-e” so I skipped it. Perhaps a prompt like “insert code” or similar would be helpful for we Noobs. Oh well.
Sorry about the image, I tried to copy the results from the command window, but it kept truncating.

I always add extensive notation/documentation in any code I write when the final version. Sometimes when I come back to something six-months later, I can get confused.

Also, after contemplation, $OUs would be better titled $Sites in my environment.

Thanks for the “-WhatIf” tip. I had no idea, that’s really helpful.

—K

That would be less necessary if you write your code as verbose as possible. For someone knowing PowerShell a command line like …

New-GPLink -Name $GPOName -Target $TgT

… is a kind of self explanatory. But if the line before that …

$TgT="OU=users," + "OU=$_," + "DC=ourdomain,DC=local"

… happens to be not next to the line where you use the variable might deserve some explanation or improvement. And it would be easier to understand like this …

$TargetOU = 'OU=users,OU={0},DC=ourdomain,DC=local' -f $_

… I think.

In this case I’d use $SiteList instead of $Sites

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.