Src/Private/Get-AbrADSiteReplication.ps1

function Get-AbrADSiteReplication {
    <#
    .SYNOPSIS
    Used by As Built Report to retrieve Microsoft AD Domain Sites Replication information.
    .DESCRIPTION
 
    .NOTES
        Version: 0.7.11
        Author: Jonathan Colon
        Twitter: @jcolonfzenpr
        Github: rebelinux
    .EXAMPLE
 
    .LINK
 
    #>

    [CmdletBinding()]
    param (
        [Parameter (
            Position = 0,
            Mandatory)]
            [string]
            $Domain
    )

    begin {
        Write-PscriboMessage "Collecting AD Domain Sites Replication information."
    }

    process {
        Write-PscriboMessage "Collecting AD Domain Sites Replication Summary. (Sites Replication Connection)"
        $DCs = Invoke-Command -Session $TempPssSession -ScriptBlock {Get-ADDomain -Identity $using:Domain | Select-Object -ExpandProperty ReplicaDirectoryServers}
        if ($DCs) {
            Write-PscriboMessage "Discovering Active Directory Sites Replication information on $Domain. (Sites Replication)"
            try {
                $ReplInfo = @()
                foreach ($DC in $DCs) {
                    if (Test-Connection -ComputerName $DC -Quiet -Count 1) {
                        $Replication = Invoke-Command -Session $TempPssSession -ScriptBlock {Get-ADReplicationConnection -Server $using:DC -Properties *}
                        if ($Replication) {
                            try {
                                Write-PscriboMessage "Collecting Active Directory Sites Replication information on $DC. (Sites Replication Connection)"
                                foreach ($Repl in $Replication) {
                                    try {
                                        $inObj = [ordered] @{
                                            'GUID' = $Repl.ObjectGUID
                                            'Description' = ConvertTo-EmptyToFiller $Repl.Description
                                            'Replicate From Directory Server' = ConvertTo-ADObjectName $Repl.ReplicateFromDirectoryServer.Split(",", 2)[1] -Session $TempPssSession -DC $DC
                                            'Replicate To Directory Server' = ConvertTo-ADObjectName $Repl.ReplicateToDirectoryServer -Session $TempPssSession -DC $DC
                                            'Replicated Naming Contexts' = $Repl.ReplicatedNamingContexts
                                            'Transport Protocol' = $Repl.InterSiteTransportProtocol
                                            'Auto Generated' =  ConvertTo-TextYN $Repl.AutoGenerated
                                            'Enabled' =  ConvertTo-TextYN $Repl.enabledConnection
                                            'Created' = ($Repl.Created).ToUniversalTime().toString("r")
                                        }
                                        $ReplInfo += [pscustomobject]$inobj

                                        if ($HealthCheck.Site.Replication) {
                                            $ReplInfo | Where-Object { $_.'Enabled' -ne 'Yes'} | Set-Style -Style Warning -Property 'Enabled'
                                        }
                                    }
                                    catch {
                                        Write-PscriboMessage -IsWarning "$($_.Exception.Message) (Site Replication Connection Item)"
                                    }
                                }
                            }
                            catch {
                                Write-PscriboMessage -IsWarning "$($_.Exception.Message) (Site Replication Connection Section)"
                            }
                        }
                    }
                }
                if ($ReplInfo) {
                    if ($InfoLevel.Domain -ge 2) {
                        Section -Style Heading5 'Sites Replication Connection' {
                            Paragraph "The following section provides detailed information about Site Replication Connection."
                            BlankLine
                            foreach ($Repl in ($ReplInfo | Sort-Object -Property 'Replicate From Directory Server')) {
                                Section -Style NOTOCHeading5 -ExcludeFromTOC "From: $($Repl.'Replicate From Directory Server') To: $($Repl.'Replicate To Directory Server')" {
                                    $TableParams = @{
                                        Name = "Site Replication - $($Repl.'Replicate To Directory Server')"
                                        List = $true
                                        ColumnWidths = 40, 60
                                    }
                                    if ($Report.ShowTableCaptions) {
                                        $TableParams['Caption'] = "- $($TableParams.Name)"
                                    }
                                    $Repl | Table @TableParams
                                }
                            }
                        }
                    } else {
                        Section -Style Heading5 'Sites Replication Connection' {
                            Paragraph "The following section provide summary information about Site Replication Connection."
                            BlankLine
                            $TableParams = @{
                                Name = "Site Replication - $($Domain.ToString().ToUpper())"
                                List = $false
                                Columns = 'Replicate From Directory Server', 'Replicate To Directory Server', 'Transport Protocol', 'Auto Generated', 'Enabled'
                                ColumnWidths = 28, 27, 15, 15, 15
                            }
                            if ($Report.ShowTableCaptions) {
                                $TableParams['Caption'] = "- $($TableParams.Name)"
                            }
                            $ReplInfo | Sort-Object -Property 'Replicate From Directory Server' | Table @TableParams
                        }
                    }
                }
            }
            catch {
                Write-PscriboMessage -IsWarning "$($_.Exception.Message) (Site Replication Connection)"
            }
        }
        try {
            if ($HealthCheck.Site.Replication) {
                Write-PscriboMessage "Discovering Active Directory Sites Replication Status on $Domain. (Sites Replication Status)"
                $DC = Invoke-Command -Session $TempPssSession {Get-ADDomain -Identity $using:Domain | Select-Object -ExpandProperty ReplicaDirectoryServers | Select-Object -First 1}
                $DCPssSession = New-PSSession $DC -Credential $Credential -Authentication $Options.PSDefaultAuthentication
                Write-PscriboMessage "Discovered Active Directory Sites Replication Status on $Domain. (Sites Replication Status)"
                $RepStatus =  Invoke-Command -Session $DCPssSession -ScriptBlock {repadmin /showrepl /repsto /csv | ConvertFrom-Csv}
                if ($RepStatus) {
                    Section -Style Heading5 'Sites Replication Status' {
                        $OutObj = @()
                        foreach ($Status in $RepStatus) {
                            try {
                                Write-PscriboMessage "Collecting Active Directory Sites Replication Status from $($Domain). (Sites Replication Status)"
                                $inObj = [ordered] @{
                                    'Source DSA' = $Status.'Source DSA'
                                    'Destination DSA' = $Status.'Destination DSA'
                                    'Source DSA Site' = $Status.'Source DSA Site'
                                    'Last Success Time' =  $Status.'Last Success Time'
                                    'Last Failure Status' = $Status.'Last Failure Status'
                                    'Last Failure Time' = $Status.'Last Failure Time'
                                    'Number of failures' = $Status.'Number of Failures'
                                }
                                $OutObj += [pscustomobject]$inobj

                            }
                            catch {
                                Write-PscriboMessage -IsWarning "$($_.Exception.Message) (Site Replication Status)"
                            }
                        }
                        if ($HealthCheck.Site.Replication) {
                            $OutObj | Where-Object {$_.'Last Failure Status' -gt 0} | Set-Style -Style Warning -Property 'Last Failure Status'
                        }

                        $TableParams = @{
                            Name = "Site Replication Status - $($Domain.ToUpper())"
                            List = $false
                            ColumnWidths = 14, 14, 14, 15, 14, 15 ,14
                        }
                        if ($Report.ShowTableCaptions) {
                            $TableParams['Caption'] = "- $($TableParams.Name)"
                        }
                        $OutObj| Sort-Object -Property 'Source DSA' | Table @TableParams
                        if ($HealthCheck.Site.Replication -and ($OutObj | Where-Object {$_.'Last Failure Status' -gt 0})) {
                            Paragraph "Health Check:" -Italic -Bold -Underline
                            Paragraph "Best Practices: Replication failure can lead to object inconsistencies and major problems in Active Directory." -Italic -Bold
                            BlankLine
                        }
                    }
                }
            }
        }
        catch {
            Write-PscriboMessage -IsWarning "$($_.Exception.Message) (Site Replication Status)"
        }
    }

    end {}

}