The purpose of this project is to receive every day an email containing the status of the Active Directory database backup of all the domain controllers in a forest.

I use the result from the following command :

repadmin /showbackup *

The result of this command is formatted and is sent by email.

The report contains 3 parts :

  • backup problem : the backup age is older than 2 days
  • error messages : error messages when running the repadmin command on a specific domain controller
  • backup ok

This script can be scheduled to run daily on a server with valid credential to run the repadmin command.
Script :

repadmin /showbackup * > c:\temp\systemstate_backup_status.txt

Function send_mail([string]$message,[string]$subject) {
$emailFrom = "emailfrom@domain.local"
$emailTo = "rcpTo@domain.local"
$emailCC = "rcp1@domain.local,rcp2@domain.local"
$smtpServer = "smtp_srv.domain.local"
Send-MailMessage -SmtpServer $smtpServer -To $emailTo -Cc $emailCC -From $emailFrom -Subject $subject -Body $message -BodyAsHtml
}

$date = Get-Date -Format "yyyy-MM-dd"
$report = gc c:\temp\systemstate_backup_status.txt
$message = "<HTML><HEAD>
<style type='text/css'>
table.tftable {font-family:verdana,arial,sans-serif;font-size:12px;color:#333333;border-width: 1px;border-color: #729ea5;border-collapse: collapse;}
table.tftable th {font-family:verdana,arial,sans-serif;font-size:12px;background-color:#acc8cc;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;text-align:left;}
table.tftable tr {font-family:verdana,arial,sans-serif;background-color:#d4e3e5;}
table.tftable td {font-family:verdana,arial,sans-serif;font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #729ea5;}
</style>
</HEAD><body>"

$message_ok = @()
$message_ok += "<table class='tftable' border='1'><tr><th><p>Domain</p></th><th><p>Hostname</p></th><th><p>AD Partition</p></th><th><p>Last backup</p></th></tr>"
$message_ko = "<table class='tftable' border='1'><tr><th><p>Hostname</p></th><th><p>AD Partition</p></th><th><p>Last backup</p></th></tr>"
$message_error = "<table class='tftable' border='1'><tr><th><p>Hostname</p></th><th><p>Error message</p></th></tr>"

foreach ($line in $report) {
if ($line -ne "") {
if ($line -like "Repadmin: *") {
$fqdn = $line.Split(" ")[-1]
}
if ($line -like "*dSASignature") {
$index = [array]::IndexOf($report, $line)
if ($line -like "*dSASignature") {
$zone = $report[($index-2)]
}
$backup_date = $line.Split(" ",[StringSplitOptions]'RemoveEmptyEntries')[3]
$diff = ((Get-Date $date) - (Get-Date $backup_date)).days

if ($diff -lt 2) {
$Hostname = $fqdn.split(".")[0]
$Domain = $fqdn.split(".")[1..(($fqdn.length) -1)] -join "."
$message_ok += "<tr><td><p><font color=green>" + $Domain + "</p></td><td><p>" + $Hostname + "</p></td><td><p>" + $zone + "</p></td><td><p>" + $backup_date + "</p></td></tr>"
}
else {
$message_ko += "<tr><td><font color=red>" + $fqdn + "<td>" + $zone + "<td>" + $backup_date
}
}
if ($line -like "*error*") {
$message_error += "<tr><td><font color=red>" + $fqdn + "<td>" + $line
}
}
}

$message_ok = $message_ok | sort

if ($message_ko -eq "<table class='tftable' border='1'><tr><th><p>Hostname</p></th><th><p>AD Partition</p></th><th><p>Last backup</p></th></tr>") {
$message_ko = "<table style='color:gray;font-family:verdana,arial,sans-serif;font-size:11px;'>No problem detected</table>"
}
$subject = "Active Directory backup status : "+$date
$message += "<font face='Calibri' color='black'><i><b>AD backup problem (more than 2 days) :</b></i><br>" + `
$message_ko + "</table><br>" + `
"<font face='Calibri' color='black'><i><b>Problem to contact the following domain controller(s) :</b></i><br>" + `
$message_error + "</table><br>" + `
"<font face='Calibri' color='black'><i><b>AD backup OK :</b></i><br>" + `
$message_ok + "</table><br></body>"
send_mail $message $subject

 

Monitor Active Directory database backup + report sent by email

Leave a Reply

Your email address will not be published.