Test-ADHealth.psm1

function Test-ADHealth {
    #Param([String]$IP,[string]$Domain)
    Param(
        # Email address from which email will be sent
        [Parameter(Mandatory=$true)]
        [string]
        $FromAddress,

        # Email address to which report will be sent
        [Parameter(Mandatory=$true)]
        [string]
        $ToAddress,

        # SMTP Server Address
        [Parameter()]
        [string]
        $SMTPServer="outlook.office365.com",

        # Credential File Path
        [Parameter()]
        [string]
        $CredFile
    )
  ##process block is required to process all the inputs from pipeline one by one, otherwise it will just process last one
    process{
        #Get info about domain current computer is connected to
        Import-Module ActiveDirectory
        $domain = Get-ADDomain | Select-Object -ExpandProperty Forest
        # Check for replication failures
        try {
            $replicationFailure = Get-ADReplicationFailure -Target $domain -Scope Forest        
        }
        catch [System.TimeoutException] {
            $checkTimedOut = $true            
        }
        finally{
            $Creds = Import-Clixml -Path $CredFile
            if($replicationFailure){
                $Subject = "AD replication failed for $domain"
                $Body = $replicationFailure | ConvertTo-Html
                Send-MailMessage -from $FromAddress -to $ToAddress -subject $Subject -BodyAsHtml $Body -SmtpServer $SMTPServer -Credential $Creds -UseSsl -Port 587 -DeliveryNotificationOption Never
            }
            elseif ($checkTimedOut) {
                $Subject = "AD replication check failed with a timeout for $domain" 
                $Body = "The automated replication check failed to complete, please review the AD replication manually for $domain"
                Send-MailMessage -from $FromAddress -to $ToAddress -subject $Subject -body $Body -SmtpServer $SMTPServer -Credential $Creds -UseSsl -Port 587 -DeliveryNotificationOption Never
            }
        }
    }
  
  
  
  <#
  .SYNOPSIS
  Function to get alerts on failed AD replication
   
  .DESCRIPTION
  Use this function to schedule task that checks for AD replication status and send an email if it finds out any failures
  .EXAMPLE
   
  .EXAMPLE
   
  #>

  }