• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1572
  • Last Modified:

Replace newlines

Hi,

I've the below code to retrieve the content from a text and then email it out as HTML. However, the replacement on the carriage returns or new lines is not working and hence my email becomes a continuous wall of text without any new lines.

Can advise where I am wrong? I just want to replace all the line breaks to to HTML <br> tags.

$errorContent=Get-Content $script:logfile
$subject = "Batch Job Error : CMSDailyReminder"
$body = "Excution Date : "+$StartTime
$body += "<br><br>" + $errorContent
$body = $body -replace "`r`n", "<br>"
send-MailMessage -SmtpServer $smtp -To $spAdminEmail -From $from -Subject $subject -Body $body -BodyAsHtml -Priority High

Open in new window

0
NicksonKoh
Asked:
NicksonKoh
  • 2
1 Solution
 
Joe KlimisCommented:
Hi

The problem is you are creating an array , with the +=

add the strings together like this

$errorContent = Get-Content $script:logfile
$subject = "Batch Job Error : CMSDailyReminder"
$body = "Excution Date : "+$StartTime
$body = $body ,"<br><br>" + $errorContent
$body = $body -replace "`r`n", "<br>"
send-MailMessage -SmtpServer $smtp -To $spAdminEmail -From $from -Subject $subject -Body $body -BodyAsHtml -Priority High

Open in new window

0
 
footechCommented:
There is more than one way to handle this (as usual).  Here's a couple methods.
$errorContent = Get-Content $script:logfile
$subject = "Batch Job Error : CMSDailyReminder"
$body = "Excution Date : "+$StartTime
$body += "<br><br>" + ($errorContent -join "<br>")
Send-MailMessage -SmtpServer $smtp -To $spAdminEmail -From $from -Subject $subject -Body $body -BodyAsHtml -Priority High

Open in new window

In this one $errorContent is an array.  We use the -join operator on it to join all the members of the array into one string.


$errorContent = Get-Content $script:logfile | Out-String
$subject = "Batch Job Error : CMSDailyReminder"
$body = "Excution Date : "+$StartTime
$body += "<br><br>" + $errorContent
$body = $body -replace "\r\n", "<br>"
Send-MailMessage -SmtpServer $smtp -To $spAdminEmail -From $from -Subject $subject -Body $body -BodyAsHtml -Priority High

Open in new window

In this one, the content of the logfile is piped to Out-File to create a single string instead of an array.  Take note that when using the -replace operator you are using RegEx, so \r and \n are needed to match carriage return and newline, respectively.  If you were using the Replace method, then you would use the escaped characters in PS like below:
$body = $body.replace("`r`n","<br>")
0
 
NicksonKohAuthor Commented:
That's crystal clear explanation footech! You are a very good teacher.
0
 
footechCommented:
Thanks!
Unfortunately, the last bit contains an error (which I'd like to correct).
Take note that when using the -replace operator you are using RegEx, so \r and \n are needed to match carriage return and newline, respectively.  If you were using the Replace method, then you would use the escaped characters in PS like below:
When using the -replace operator, you can actually use either the RegEx notation or the PS escaped notation (both will work).  When using the replace method however, only the escaped notation works.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now