Good Morning everyone. I hope you can help (pretty sure you can). I am working on a powershell script to copy the OU structure from an existing OU and create a new one and then apply the same group policies to the newly created OU. I have the creation portion working fine. The script creates the OU and all sub OU's however I have not been able to figure out how to get the GP links to copy from anything other than the root OU. I found this snippet online but I can only get it to work on the root of the OU. The full script is at the bottom. Right now I am just manually applying the links but I would like to not have to edit this script every time a new GPO is created so I want it to read all of the GP links from the whole OU structure like the first part of the full script does. Thanks in advance for any and all help!
Roger
import-module GroupPolicy
$SourceOU = "Computers"
$DestinationOU = "OU=computers,OU=MSP,OU=NA,DC=testeucs,DC=local"
[xml]$gpos = Get-GPOReport -all -ReportType xml
foreach ($gpo in $gpos.gpos.gpo) {
if ($gpo.LinksTo.SOMName -eq $SourceOU) {
New-GPLink -Name $gpo.name -Target $DestinationOU
}
}#--------Config
$RegionOU = Read-Host "Enter Region: NA, ASIA, or EULA"
$NewSiteOU = Read-Host "Enter name of new site"
$sourceOU = "OU=NBKWin7,OU=NA,DC=testeucs,DC=local"
$destinationOU = "OU=$NewSiteOU,OU=$RegionOU,DC=testeucs,DC=local"
$DestCompOU = "ou=Computers,$destinationOU"
$DestUserOU = "OU=Users,$destinationOU"
#--------Main
$adPath= "LDAP://" + $destinationOU
import-module activedirectory
Import-Module GroupPolicy
#Create OUs
$objDomain=New-Object System.DirectoryServices.DirectoryEntry($adPath)
$ObjSearch=New-Object System.DirectoryServices.DirectorySearcher($ObjDomain)
[array] $OUs = @()
$OUs = dsquery * $sourceOU -Filter "(objectCategory=organizationalUnit)" -limit 0
$OUsorted = $OUs | sort-object { $_.Length}
for ($k=0; $k -le $OUsorted.Count -1; $k++)
{
$OUtoCreate = ($OUsorted[$k] -replace $sourceOU,$destinationOU).ToString()
$OUSearch = ($OUtoCreate -replace '"',"").ToString()
$ObjSearch.Filter = "(&(objectCategory=organizationalUnit)(distinguishedName="+ $OUSearch + "))"
$allSearchResult = $ObjSearch.FindAll()
if ($allSearchResult.Count -eq 1)
{"No changes were done on = " + $OUtoCreate
}
else
{
dsadd ou $OUtoCreate"OU Creation = " + $OUtoCreate
}
}
New-GPLink -Name KerberosDESEncryption -Target $destinationOU -Domain testeucs.local -LinkEnabled Yes -Order 1
New-GPLink -Name WIN8_InternetExplorer -Target $destinationOU -Domain testeucs.local -LinkEnabled Yes -Order 2
New-GPLink -Name WIN7_DCMSENABLE -Target $DestCompOU -Domain testeucs.local -LinkEnabled Yes -Order 1
New-GPLink -Name DNS_Suffix -Target $DestCompOU -Domain testeucs.local -LinkEnabled Yes -Order 2