Script for Monitor Active Directory Replica

Here you find a powershell script that use repadmin to check AD replication status and send you the output if some errors is found.

$LargestDeltaTreshold = 60

$repadmin = repadmin /replsum

[regex]$regex = '\s+(?\S+)\s+(?\S+)\s+(?\d{1,2}\s)'

[regex]$regex2 = '\s+(?\d{1,2}\S+)\s\-\s+(?\S+)'

$from = "AD Replication Status"
$to = "myemail@address.com"
$toError = "myemail_1@address.com", "myemail_2@address.com"
$repadmin | ForEach-Object {
    if ( $_ -match $regex ) {
        $process = "" | Select-Object DC, Delta, fail
        $process.dc = $matches.dc
        $process.Delta = $matches.Delta
        $process.fail = [int]($matches.fail)
        $VdayTime = 0
        $VHourTime = 0
        $VMinutesTime = 0
        
        if ($process.Delta.contains("d"))  {
		  $VdayTime = [int]($process.Delta.substring(0,2))
		  $VHourTime = [int]($process.Delta.substring(4,2))
		  $VMinutesTime = [int]($process.Delta.substring(8,2))}
		Else {		
		  if ($process.Delta.contains("h"))  {
          $VHourTime = [int]($process.Delta.substring(0,2))
          $VMinutesTime = [int]($process.Delta.substring(4,2))}
          Else {
            if ($process.Delta.contains("m"))  {
                $VMinutesTime = [int]($process.Delta.substring(0,2))}
          }
	    }

        $DeltaMinutes = New-TimeSpan -Days $VdayTime -Hours $VHourTime -Minutes $VMinutesTime

        if (($DeltaMinutes.Minutes -gt $LargestDeltaTreshold) -or ($process.fail -gt 0)) {
            $errorCount = $errorCount + 1
        }

    }

    Elseif ( $_ -match $regex2 ) {
		$errorCount = $errorCount + 1
    }
}
If ($errorCount -ne $null) {
    $emailsubject = "Daily Forest Replication Status - Some Errors"
    Send-MailMessage -From $from -To $toError -Subject $emailsubject -SmtpServer "smtp_server.domain.grp" -Body ($repadmin | Out-String)}
else {
    $emailsubject = "Daily Forest Replication Status - No Replication Error"
    Send-MailMessage -From $from -To $to -Subject $emailsubject -SmtpServer "smtp_server.domain.grp" -Body ($repadmin | Out-String)
}

Update: new script here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s