So, as part of my advanced Active Directory module called ADEssentials, I wrote a function that uses repadmin to generate results. Then, I use PSWriteHTML to process them and Mailozaurr to send them to my email. Here's a how I did it:
$ReplicationSummary = Get-WinADForestReplicationSummary -IncludeStatisticsVariable Statistics
$Body = EmailBody {
EmailImage -Source 'https://evotec.xyz/wp-content/uploads/2021/04/Logo-evotec-bb.png' -UrlLink '' -AlternativeText 'Logo' -Width 181 -Heigh 57 -Inline
EmailText -Text "Dear ", "AD Team," -LineBreak
EmailText -Text "Upon reviewing the resuls of replication I've found: "
EmailList {
EmailListItem -Text "Servers with good replication: ", $($Statistics.Good) -Color Black, SpringGreen -FontWeight normal, bold
EmailListItem -Text "Servers with replication failures: ", $($Statistics.Failures) -Color Black, Red -FontWeight normal, bold
EmailListItem -Text "Servers with replication delta over 24 hours: ", $($Statistics.DeltaOver24Hours) -Color Black, Red -FontWeight normal, bold
EmailListItem -Text "Servers with replication delta over 12 hours: ", $($Statistics.DeltaOver12Hours) -Color Black, Red -FontWeight normal, bold
EmailListItem -Text "Servers with replication delta over 6 hours: ", $($Statistics.DeltaOver6Hours) -Color Black, Red -FontWeight normal, bold
EmailListItem -Text "Servers with replication delta over 3 hours: ", $($Statistics.DeltaOver3Hours) -Color Black, Red -FontWeight normal, bold
EmailListItem -Text "Servers with replication delta over 1 hour: ", $($Statistics.DeltaOver1Hours) -Color Black, Red -FontWeight normal, bold
EmailListItem -Text "Unique replication errors: ", $($Statistics.UniqueErrors.Count) -Color Black, Red -FontWeight normal, bold
}
if ($Statistics.UniqueErrors.Count -gt 0) {
EmailText -Text "Unique replication errors:"
EmailList {
foreach ($ErrorText in $Statistics.UniqueErrors) {
EmailListItem -Text $ErrorText
}
}
} else {
EmailText -Text "It seems you're doing a great job! Keep it up! 😊" -LineBreak
}
EmailText -Text "For more details please check the table below:"
EmailTable -DataTable $ReplicationSummary {
EmailTableCondition -Inline -Name "Fail" -HighlightHeaders 'Fails', 'Total', 'PercentageError' -ComparisonType number -Operator gt 0 -BackgroundColor Salmon -FailBackgroundColor SpringGreen
} -HideFooter
EmailText -LineBreak
EmailText -Text "Kind regards,"
EmailText -Text "Your automation friend"
}
$EmailSplat = @{
From = 'przemyslaw.klys@evotec.pl'
To = 'przemyslaw.klys@evotec.pl'
Body = $Body
Priority = if ($Statistics.Failures -gt 0) { 'High' } else { 'Low' }
Subject = 'Replication Results 💖'
Verbose = $true
WhatIf = $false
MgGraph = $true
}
Connect-MgGraph
Send-EmailMessage @EmailSplat
What is the result of those 50 lines of code?
The Get-WinADForestReplicationSummary function joins over 100 functions available for Active Directory admins in the ADEssentials module. It's doing the heavy lifting of reading repadmin data and converting it to PowerShell objects. Then, we use the PSWriteHTML EmailBody function, which allows for the accessible building of emails without knowing HTML and CSS. Finally, since I wanted to send an email with Microsoft Graph, I've used Mailozaurr's amazing Send-EmailMessage to send an email.