Powershell Script for AD GPO Replication:
Purpose: I got 1 project request that i have to replicate the current OU Structure to Another location/OU in same domain. So i wrote a script to replicate the OU structure, later customer requested to link the GPOs as it was in Source OU to target OU. so i wrote below script.
OU replication powershell script i will post in another post.
Ex:
SourceOU:
TestOU1
GPOLink1
GPOLink1
OU2
GPOLink3
GPOLink4
TargetOU:
TestOU2
GPOLink1
GPOLink1
OU2
GPOLink3
GPOLink4
Script:
# Import the Active Directory module
import-module activedirectory
# Import the Group Policy module
Import-Module GroupPolicy
# Source for GPO links
$Source = "<OU Path;Ex: OU=Test1,DC=XX,DC=XXXXX,DC=com>"
# Target where we want to set the new links
$Target = "<OU Path;Ex: OU=Test2,DC=XX,DC=XXXXX,DC=com>"
##################
#Main Function
##################
[array] $OUs = @()
$OUs = dsquery * $Source -Filter "(objectCategory=organizationalUnit)" -limit 0
$OUsorted = $OUs | sort-object { $_.Length}
for ($k=0; $k -le $OUsorted.Count -1; $k++)
{
#$OUsorted[4]
$sourceOuSearck = $OUsorted[$k].ToString()
$sourceOulink = ($sourceOuSearck -replace '"',"").ToString()
$linked = (Get-GPInheritance -Target $sourceOulink).gpolinks
$targetlinkpoint = ($OUsorted[$k] -replace $Source,$Target).ToString()
$TargetOULink = ($targetlinkpoint -replace '"',"").ToString()
# Loop through each GPO and link it to the target
foreach ($link in $linked)
{
$guid = $link.GPOId
$order = $link.Order
$enabled = $link.Enabled
if ($enabled)
{
$enabled = "Yes"
# Create the link on the target
New-GPLink -Guid $guid -Target $TargetOULink -LinkEnabled $enabled -confirm:$false
# Set the link order on the target
Set-GPLink -Guid $guid -Target $TargetOULink -Order $order -confirm:$false
}
else
{
$enabled = "No"
}
}
}
Purpose: I got 1 project request that i have to replicate the current OU Structure to Another location/OU in same domain. So i wrote a script to replicate the OU structure, later customer requested to link the GPOs as it was in Source OU to target OU. so i wrote below script.
OU replication powershell script i will post in another post.
Ex:
SourceOU:
TestOU1
GPOLink1
GPOLink1
OU2
GPOLink3
GPOLink4
TargetOU:
TestOU2
GPOLink1
GPOLink1
OU2
GPOLink3
GPOLink4
Script:
# Import the Active Directory module
import-module activedirectory
# Import the Group Policy module
Import-Module GroupPolicy
# Source for GPO links
$Source = "<OU Path;Ex: OU=Test1,DC=XX,DC=XXXXX,DC=com>"
# Target where we want to set the new links
$Target = "<OU Path;Ex: OU=Test2,DC=XX,DC=XXXXX,DC=com>"
##################
#Main Function
##################
[array] $OUs = @()
$OUs = dsquery * $Source -Filter "(objectCategory=organizationalUnit)" -limit 0
$OUsorted = $OUs | sort-object { $_.Length}
for ($k=0; $k -le $OUsorted.Count -1; $k++)
{
#$OUsorted[4]
$sourceOuSearck = $OUsorted[$k].ToString()
$sourceOulink = ($sourceOuSearck -replace '"',"").ToString()
$linked = (Get-GPInheritance -Target $sourceOulink).gpolinks
$targetlinkpoint = ($OUsorted[$k] -replace $Source,$Target).ToString()
$TargetOULink = ($targetlinkpoint -replace '"',"").ToString()
# Loop through each GPO and link it to the target
foreach ($link in $linked)
{
$guid = $link.GPOId
$order = $link.Order
$enabled = $link.Enabled
if ($enabled)
{
$enabled = "Yes"
# Create the link on the target
New-GPLink -Guid $guid -Target $TargetOULink -LinkEnabled $enabled -confirm:$false
# Set the link order on the target
Set-GPLink -Guid $guid -Target $TargetOULink -Order $order -confirm:$false
}
else
{
$enabled = "No"
}
}
}
No comments:
Post a Comment