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.6
        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 {
                Section -Style Heading5 'Sites Replication Connection' {
                    Paragraph "The following section provides detailed information about Site Replication Connection."
                    BlankLine
                    $OutObj = @()
                    foreach ($DC in $DCs) {
                        if (Test-Connection -ComputerName $DC -Quiet -Count 1) {
                            Section -ExcludeFromTOC -Style NOTOCHeading6 $DC.ToString().ToUpper().Split(".")[0] {
                                try {
                                    $Replication = Invoke-Command -Session $TempPssSession -ScriptBlock {Get-ADReplicationConnection -Server $using:DC -Properties *}
                                    if ($Replication) {
                                        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
                                                    'AutoGenerated' =  ConvertTo-TextYN $Repl.AutoGenerated
                                                    'Enabled' =  ConvertTo-TextYN $Repl.enabledConnection
                                                    'Created' = ($Repl.Created).ToUniversalTime().toString("r")
                                                }
                                                $OutObj = [pscustomobject]$inobj

                                                if ($HealthCheck.Site.Replication) {
                                                    $OutObj | Where-Object { $_.'Enabled' -ne 'Yes'} | Set-Style -Style Warning -Property 'Enabled'
                                                }

                                                $TableParams = @{
                                                    Name = "Site Replication - $($DC.ToString().ToUpper().Split(".")[0])"
                                                    List = $true
                                                    ColumnWidths = 40, 60
                                                }
                                                if ($Report.ShowTableCaptions) {
                                                    $TableParams['Caption'] = "- $($TableParams.Name)"
                                                }
                                                $OutObj | Table @TableParams
                                                if ($HealthCheck.Site.Replication -and ($OutObj | Where-Object { $_.'Enabled' -ne 'Yes'})) {
                                                    Paragraph "Health Check:" -Italic -Bold -Underline
                                                    Paragraph "Best Practices: Remove unused Sites replication connections." -Italic -Bold
                                                }
                                            }
                                            catch {
                                                Write-PscriboMessage -IsWarning "$($_.Exception.Message) (Site Replication Connection Item)"
                                            }
                                        }
                                    }
                                }
                                catch {
                                    Write-PscriboMessage -IsWarning "$($_.Exception.Message) (Site Replication Connection Section)"
                                }
                            }
                        }
                    }
                }
            }
            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] @{
                                    'Destination DSA' = $Status.'Destination DSA'
                                    'Source DSA' = $Status.'Source 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 | 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 {}

}