Solved

Replace newlines

Posted on 2013-05-09
4
1,264 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

12 Experts available now in Live!

Get 1:1 Help Now