-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncADDomain.psm1
94 lines (87 loc) · 2.73 KB
/
SyncADDomain.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#=======================================================================#
#
# Author: Collin Chaffin
# Last Modified: 11/01/2014 10:00 PM
# Filename: SyncADDomain.psm1
#
#
# Changelog:
#
# v 1.0.0.1 : 11/01/2014 : Initial release
#
# Notes:
#
# This module emulates the repadmin /syncall to force AD replication
# across all sites and domain controllers. At the time I wrote this I
# could not find any example or suitable replacement to calling the
# repadmin binary
#
#=======================================================================#
function Sync-ADDomain
{
<#
.SYNOPSIS
Emulates the repadmin /syncall to force AD replication
.DESCRIPTION
Author: Collin Chaffin
Description: This function emulates the repadmin /syncall to
force AD replication across all sites and domain
controllers. At the time I wrote this I could not
find any example or suitable replacement to calling
the repadmin binary
.EXAMPLE
C:\> Sync-ADDomain
Forcing Replication on WIN2008R2-DC1.lab.local
Forcing Replication on WIN2008R2-DC2.lab.local
Forcing Replication on WIN2008R2-DC3.lab.local
.EXAMPLE
C:\> Sync-ADDomain -WhatIf
What if: Performing operation "Forcing Replication" on Target "WIN2008R2-DC1.lab.local".
What if: Performing operation "Forcing Replication" on Target "WIN2008R2-DC2.lab.local".
What if: Performing operation "Forcing Replication" on Target "WIN2008R2-DC3.lab.local".
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param (
)
BEGIN
{
Write-Debug "Sync-ADDomain function started."
try
{
# Set up the AD object and retrieve operator's current AD domain
$adDomain = $env:userdnsdomain
Write-Debug "Detected operators AD domain as $($adDomain)"
$objADContext = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("Domain", $adDomain)
$domainControllers = [System.DirectoryServices.ActiveDirectory.DomainController]::findall($objADContext)
}
catch
{
#Throw terminating error
Throw $("ERROR OCCURRED DETERMINING USERDNSDOMAIN AND RETRIEVING LIST OF DOMAIN CONTROLLERS " + $_.Exception.Message)
}
}
PROCESS
{
try
{
# Cycle through all domain controllers emulating a repadmin /syncall
foreach ($domainController in $domainControllers)
{
if ($PSCmdlet.ShouldProcess($domainController,"Forcing Replication"))
{
Write-Host "Forcing Replication on $domainController" -ForegroundColor Cyan
$domainController.SyncReplicaFromAllServers(([ADSI]"").distinguishedName,'CrossSite')
}
}
}
catch
{
#Throw terminating error
Throw $("ERROR OCCURRED FORCING DIRECTORY SYNCHRONIZATION " + $_.Exception.Message)
}
}
END
{
Write-Debug "Sync-ADDomain function completed successfully."
}
}