0

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  
  }
200mg
  • 503
  • 1
  • 10
  • 24
  • 2
    For `Clear-Variable` reperr does not need to be in double quotes. Otherwise, in which line does your error occur? Is it in `$body`? In this case write `$($reperr)` or as a format `"{0}" -f $reperr`. – Alex_P Sep 30 '19 at 18:47
  • Alex, yes, it's in the $body. I cannot get it to expand using your method either. When i get the email the body is empty. If I just type $reperr on the cli after this runs it does have a value. $body = @" $($reperr) "@ – 200mg Sep 30 '19 at 19:41
  • 1
    Objects in a string would need to be `"$($reperr)"`. Actually, you probably want to pipe to out-string first. – js2010 Sep 30 '19 at 20:25

0 Answers0