Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Replace newlines

Posted on 2013-05-09
4
Medium Priority
?
1,507 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 11

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 41

Accepted Solution

by:
footech earned 2000 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 41

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

Are your AD admin tools letting you down?

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

A procedure for exporting installed hotfix details of remote computers using powershell
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

609 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