Using PowerShell to perform DC health checks (DCDIAG, REPADMIN)

Below you can find another great and useful PowerShell function for checking DC health (DCDIAG, Repadmin) remotely. Please also check out one of the latest article for creating basic DC health email report.

DC health checks

There are several commands known for basic Domain Controller troubleshooting. In this function we focused on DCDIAG and Repadmin as they are used probably in every situation.

(Dcdiag.exe /s:$Server) -split ('[\r\n]')
(repadmin /showrepl /repsto /csv | ConvertFrom-Csv)

On the beginning of script I added try/catch to check if server exist and then test connectivity.


    Try
    {
        $DC = Get-ADDomainController -Identity $Server
    }
    Catch
    {
        Write-Host "Error: " -NoNewline -ForegroundColor Yellow
        Write-Host $_.Exception.Message
        Write-Host
        Break
    }

    # Testing connection
    If(!(Test-Connection -Cn $Server -BufferSize 16 -Count 1 -ea 0 -Quiet))
    {
        Write-Warning   "Failed to connect to $Server"
    }
    Else
    {
     
     .........

    }

For DCDIAG part I used some lines from “Active Directory Health Check” script.
Usage:

Get-DCHealth -Server DC01 -Check DCDIAG
Get-DCHealth -Server DC01 -Check Repadmin

Final script:


function Get-DCHealth {
    [CmdletBinding()]
       

# Parameters used in this function
    param
    (
        [Parameter(Position=0, Mandatory = $true, HelpMessage="Provide server name", ValueFromPipeline = $true)] 
        $Server,

        [Parameter(Position=1, Mandatory = $true, HelpMessage="Select DC health check (DCDIAG, Repadmin)", ValueFromPipeline = $true)][ValidateSet("DCDIAG", "Repadmin")][string]
        $Check
    ) 

# Checking if server exist
    Try
    {
        $DC = Get-ADDomainController -Identity $Server
    }
    Catch
    {
        Write-Host "Error: " -NoNewline -ForegroundColor Yellow
        Write-Host $_.Exception.Message
        Write-Host
        Break
    }

# Testing connection
    If(!(Test-Connection -Cn $Server -BufferSize 16 -Count 1 -ea 0 -Quiet))
    {
        Write-Warning   "Failed to connect to $Server"
    }
    Else
    {
        If($Check -eq "DCDIAG")
        {
            $AllDCDiags = @()
            Write-Host "DCDIAG results for"$Server":" -ForegroundColor Yellow  -NoNewline
                            
            $Dcdiag = (Dcdiag.exe /s:$Server) -split ('[\r\n]')
            $Results = New-Object Object
            $Results | Add-Member -Type NoteProperty -Name "ServerName" -Value $Server
            $Dcdiag | %{ 
            
                Switch -RegEx ($_) 
                { 
                    "Starting"      { $TestName   = ($_ -Replace ".*Starting test: ").Trim() } 
                    "passed test|failed test" 
                    { 
                        If ($_ -Match "passed test")
                        {  
                            $TestStatus = "Passed"  
                        }  
                        Else  
                        {   
                            $TestStatus = "Failed"  
                        } 
                    } 
                } 
            
                If ($TestName -ne $Null -And $TestStatus -ne $Null) 
                { 
                    $Results | Add-Member -Name $("$TestName".Trim()) -Value $TestStatus -Type NoteProperty -force 
                    $TestName = $Null; $TestStatus = $Null       
                } 
            }       
                $AllDCDiags += $Results 
                $AllDCDiags  | fl | Out-String
        }
        ElseIf($Check -eq "Repadmin")
        {
            $repadmin = @()

            Write-Host "REPADMIN results for"$Server":"  -ForegroundColor Yellow -NoNewline
            Write-Host " "
            $rep = (Invoke-Command $Server -ScriptBlock{repadmin /showrepl /repsto /csv | ConvertFrom-Csv})

            $rep | ForEach-Object {
    
            # Define current loop to variable
            $r = $_

            # Adding properties to object
            $REPObject = New-Object PSCustomObject
            $REPObject | Add-Member -Type NoteProperty -Name "Destination DCA" -Value $r.'destination dsa'
            $REPObject | Add-Member -Type NoteProperty -Name "Source DSA" -Value $r.'source dsa'
            $REPObject | Add-Member -Type NoteProperty -Name "Source DSA Site" -Value $r."Source DSA Site" 
            $REPObject | Add-Member -Type NoteProperty -Name "Last Success Time" -Value $r.'last success time'
            $REPObject | Add-Member -Type NoteProperty -Name "Last Failure Status" -Value $r.'Last Failure Status'
            $REPObject | Add-Member -Type NoteProperty -Name "Last Failure Time" -Value $r.'last failure time'
            $REPObject | Add-Member -Type NoteProperty -Name "Number of failures" -Value $r.'number of failures'

            # Adding object to array
            $repadmin += $REPObject

            }
            $repadmin | ft  | Out-String
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.