Solved

Replace newlines

Posted on 2013-05-09
4
1,338 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:Joe Klimis
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 40

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 40

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Synchronize a new Active Directory domain with an existing Office 365 tenant
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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