Skip to content

Instantly share code, notes, and snippets.

@realslacker
Created February 18, 2021 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save realslacker/c2c3afa235804b4a82b235fe5fc17eee to your computer and use it in GitHub Desktop.
Save realslacker/c2c3afa235804b4a82b235fe5fc17eee to your computer and use it in GitHub Desktop.
[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