Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get Active Directory Replication Status
function Get-AdReplicationStatus()
{
<#
.SYNOPSIS
This function returns the status of AD replication.
.PARAMETER AdServer
Specifies the name of the AD Server to query the status of. The default is the local machine.
.PARAMETER SiteName
If specified, then only the status for the given AD site is returned. Example: Default-First-Site-Name
.PARAMETER Partition
If specified, then only the status for the given AD partitions are returned. Example: DC=domain,DC=com
.EXAMPLE
Get-AdReplicationStatus | FT -Autosize
# Gets the status of all sites and partitions on the local AD server.
SiteName DomainController Name DsaGuid LastAttempt
-------- ---------------- ---- ------- -----------
Default-First-Site-Name domain-dc1 DC=domain,DC=com c871f908-8e3c-46b0-bb03-01411810433d 6/5/2017 9:42:26 AM
Site2 domain-dc2 DC=domain,DC=com 7d55876d-3cc8-405a-bd5c-375447254875 6/5/2017 9:50:43 AM
Site2 domain-dc2 CN=Configuration,DC=domain,DC=com 7d55876d-3cc8-405a-bd5c-375447254875 6/5/2017 9:24:20 AM
Default-First-Site-Name domain-dc1 CN=Configuration,DC=domain,DC=com c871f908-8e3c-46b0-bb03-01411810433d 6/5/2017 9:42:27 AM
Site2 domain-dc2 CN=Schema,CN=Configuration,DC=domain,DC=com 7d55876d-3cc8-405a-bd5c-375447254875 6/5/2017 9:24:18 AM
Default-First-Site-Name domain-dc1 CN=Schema,CN=Configuration,DC=domain,DC=com c871f908-8e3c-46b0-bb03-01411810433d 6/5/2017 9:42:27 AM
Default-First-Site-Name domain-dc1 DC=DomainDnsZones,DC=domain,DC=com c871f908-8e3c-46b0-bb03-01411810433d 6/5/2017 9:42:27 AM
Site2 domain-dc2 DC=DomainDnsZones,DC=domain,DC=com 7d55876d-3cc8-405a-bd5c-375447254875 6/5/2017 9:43:00 AM
Site2 domain-dc2 DC=ForestDnsZones,DC=domain,DC=com 7d55876d-3cc8-405a-bd5c-375447254875 6/5/2017 9:27:19 AM
Default-First-Site-Name domain-dc1 DC=ForestDnsZones,DC=domain,DC=com c871f908-8e3c-46b0-bb03-01411810433d 6/5/2017 9:42:27 AM
#>
param
(
[string] $AdServer = $ENV:COMPUTERNAME,
[string] $SiteName,
[string[]] $Partition
)
$session = New-PsSession -ComputerName $AdServer
try
{
$replOutput = Invoke-Command -Session $session -ScriptBlock { repadmin /showrepl }
$start = $false
$new = $null
$partName = ""
foreach ( $line in $replOutput | Where-Object { $_ } )
{
if ( $line -imatch "==== INBOUND" )
{
$start = $true
}
if ( $start )
{
if ( $line -imatch "^(DC|CN)=")
{
$partName = $line.Trim()
}
elseif ( $partName -and ($line -imatch "^ .* via RPC") )
{
$new = New-Object -TypeName "PSCustomObject" -Property `
@{
"Name" = $partName
"SiteName" = ""
"DomainController" = ""
"DsaGuid" = ""
"LastAttempt" = ""
}
$new.SiteName = ($line -split "\\")[0].Trim()
$new.DomainController = (($line -split "\\")[1] -ireplace " via RPC").Trim()
}
elseif ( $new -and ($line -imatch "^ DSA object GUID:") )
{
$new.DsaGuid = ($line -isplit ": ")[1].trim()
}
elseif ( $new -and ($line -imatch "^ Last attempt ") )
{
$new.LastAttempt = [DateTime](($line -isplit " @ ")[1] -ireplace " was .*").Trim()
$new | Where-Object { (!$SiteName -or $new.SiteName -ilike $SiteName) -and (!$Partition -or $new.Name -iin $Partition) }
$new = $null
}
}
}
}
catch
{
throw $_
}
finally
{
$session | Remove-PSSession
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.