PesterInfrastructureTests.psm1

function Test-ADPester {
    Describe -Name 'Domain Controller Infrastructure Test' { try { $AllDomains = (Get-ADForest -ErrorAction Stop).Domains } catch { $AllDomains = $null }
        It -Name 'Active Directory Forest is available' { $AllDomains | Should -Not -BeNullOrEmpty }
        if ($null -eq $AllDomains) { return }
        foreach ($Domain in $AllDomains) {
            try { $DCS = (Get-ADDomainController -Server $Domain -Filter * -ErrorAction Stop | Select-Object HostName).HostName } catch { $DCS = $null }
            It -Name "Active Directory Domain $Domain is available" { $DCS | Should -Not -BeNullOrEmpty -Because 'Test' }
            if ($null -eq $DCS) { return }
            foreach ($DC01 in $DCS) {
                Context -Name "$DC01 Availability" { It -Name "$DC01 Responds to Ping" { $Ping = Test-NetConnection -ComputerName $DC01
                        $Ping.PingSucceeded | Should -Be $true }
                    It -Name "$DC01 Responds on Port 53" { $Port = Test-NetConnection -ComputerName $DC01 -Port 53
                        $Port.TcpTestSucceeded | Should -Be $true }
                    It -Name "$DC01 DNS Service is Running" { $DNSsvc = Get-Service -ComputerName $DC01 -DisplayName 'DNS Server' -ErrorAction Stop
                        $DNSsvc.Status | Should -BeExactly 'Running' }
                    It -Name "$DC01 ADDS Service is Running" { $NTDSsvc = Get-Service -ComputerName $DC01 -DisplayName 'Active Directory Domain Services' -ErrorAction Stop
                        $NTDSsvc.Status | Should -BeExactly 'Running' }
                    It -Name "$DC01 ADWS Service is Running" { $ADWSsvc = Get-Service -ComputerName $DC01 -DisplayName 'Active Directory Web Services' -ErrorAction Stop
                        $ADWSsvc.Status | Should -BeExactly 'Running' }
                    It -Name "$DC01 KDC Service is Running" { $KDCsvc = Get-Service -ComputerName $DC01 -DisplayName 'Kerberos Key Distribution Center' -ErrorAction Stop
                        $KDCsvc.Status | Should -BeExactly 'Running' }
                    It -Name "$DC01 Netlogon Service is Running" { $Netlogonsvc = Get-Service -ComputerName $DC01 -DisplayName 'Netlogon' -ErrorAction Stop
                        $Netlogonsvc.Status | Should -BeExactly 'Running' } }
                Context -Name "Replication Status" { It -Name "$DC01 Last Replication Result is 0 (Success)" { $RepResult = Get-ADReplicationPartnerMetadata -Target "$DC01" -PartnerType Both -Partition *
                        $RepResult.LastReplicationResult | Should -BeIn $null, 0 } }
            }
            Context 'Replication Link Status' { $results = repadmin /showrepl * /csv | ConvertFrom-Csv
                $groups = $results | Group-Object -Property 'Source DSA'
                foreach ($sourcedsa in $groups) {
                    Context "Source DSA = $($sourcedsa.Name)" { $targets = $sourcedsa.Group
                        $targetdsa = $targets | Group-Object -Property 'Destination DSA'
                        foreach ($target in $targetdsa) { Context "Target DSA = $($target.Name)" { foreach ($entry in $target.Group) { It "$($entry.'Naming Context') - should have zero replication failures" { $entry.'Number of failures' | Should -Be 0 } } } } }
                } }
        } }
}
Export-ModuleMember -Function @('Test-ADPester') -Alias @()