Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[CmdletBinding()]
param(
[Parameter( Mandatory, Position = 1, ValueFromPipelineByPropertyName, ValueFromPipeline )]
[Alias( 'HostName', 'Server' )]
[string[]]
$ComputerName,
[System.DirectoryServices.ActiveDirectory.SyncFromAllServersOptions]
$SyncOptions = @( 'CrossSite', 'PushChangeOutward', 'SkipInitialCheck' ),
[ArgumentCompleter({
param( $CommandName, $ParameterName, $WordToComplete, $CommandAst, $FakeBoundParameters)
if ( -not $FakeBoundParameters.ComputerName ) { return '' }
$DirectoryContext = [System.DirectoryServices.ActiveDirectory.DirectoryContext]::new( 'DirectoryServer', $FakeBoundParameters.ComputerName )
[System.DirectoryServices.ActiveDirectory.DomainController]::GetDomainController( $DirectoryContext ).Partitions.ForEach({ "'$_'" }).Where({ $_ -like "$WordToComplete*" -or $_ -like "'$WordToComplete*" })
})]
[SupportsWildcards()]
[string[]]
$Partitions = '*',
[pscredential]
$Credential
)
process {
if ( -not $PSBoundParameters.ContainsKey( 'InformationAction' ) ) { $InformationPreference = 'Continue' }
$ComputerName.ForEach({
if ( $SyncOptions -band [System.DirectoryServices.ActiveDirectory.SyncFromAllServersOptions]::PushChangeOutward ) {
Write-Information "Beginning outbound replication from server $_"
} else {
Write-Information "Beginning inbound replication to server $_"
}
if ( $Credential ) {
$DirectoryContext = [System.DirectoryServices.ActiveDirectory.DirectoryContext]::new( 'DirectoryServer', $_, $Credential.UserName, $Credential.GetNetworkCredential().Password )
} else {
$DirectoryContext = [System.DirectoryServices.ActiveDirectory.DirectoryContext]::new( 'DirectoryServer', $_ )
}
$DomainController = [System.DirectoryServices.ActiveDirectory.DomainController]::GetDomainController( $DirectoryContext )
$Partitions |
ForEach-Object { $DomainController.Partitions -like $_ } |
Select-Object -Unique |
ForEach-Object {
Write-Information "Syncing partition $_"
$DomainController.SyncReplicaFromAllServers( $_, $SyncOptions )
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment