Solved

Replace newlines

Posted on 2013-05-09
4
1,237 Views
Last Modified: 2013-05-10
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
Comment
Question by:NicksonKoh
  • 2
4 Comments
 
LVL 10

Expert Comment

by:JoeKlimis
ID: 39154600
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
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39154719
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
 
LVL 17

Author Comment

by:NicksonKoh
ID: 39154758
That's crystal clear explanation footech! You are a very good teacher.
0
 
LVL 39

Expert Comment

by:footech
ID: 39154768
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now