The variable $reperr is not expanding inside the double quoted here-string. When the scriptblock completes, $reperr does have a value, but it will not print in the here string, all i get in the body is the flat text "There are currently errors with domain.com AD replication."
Clear-Variable -name "reperr"
$reperr = Get-ADReplicationPartnerMetadata -target * |? {$_.ConsecutiveReplicationFailures -eq "0"} |select Server, ConsecutiveReplicationFailures
$smtpserver = "10.25.172.2"
#$recipients = "email@emailaddy.com"
$recipients = "user@doamin.com"
$sender = "ad_repl_status@domain.com"
$subject = "DOMAIN.COM ACTIVE DIRECTORY REPLICATION ISSUES DETECTED!"
$body = @"
There are currently errors with domain.com AD replication.
$reperr
"@
if (!$reperr)
{
Write-Host "AD REPLICATION IS CLEAN"
}
else
{
Send-MailMessage -Priority High -SmtpServer $smtpserver -From $sender -To $recipients -Subject $subject -Body $body
}