Solved

Get line breaks in powershell output to email

Posted on 2013-10-23
5
4,890 Views
Last Modified: 2013-10-23
I have a text file that is being read by powershell that has varying number of lines in it. I want powershell to read the lines and output to an email that lists each line on its own line.

The script below emails the lines all as one line and Ihave not been able to get it to show the line breaks.

Any help would be greatly appreciated.

$Line = Get-Content E:\MonitorFolder\PingResults.txt | Out-String  
 
  Send-MailMessage -From 'Lost_Network_Connection@company.com' -To 'me@systrends.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Lost Network Connection' `
  -BodyAsHtml `
    -Body @"
      $(($Line) + "'n")  
"@
0
Comment
Question by:jimmylew52
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:footech
Comment Utility
Try the following.
$Line = Get-Content E:\MonitorFolder\PingResults.txt 
 
  Send-MailMessage -From 'Lost_Network_Connection@company.com' -To 'me@systrends.com' `
  -SmtpServer 'smtp.company.com' `
  -Subject 'Lost Network Connection' `
  -BodyAsHtml `
  -Body ($Line -join "`n")

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
Comment Utility
Two lines in PingResults.txt

server is down
Station is down

Displays in email as :

server is downstation is down

instead of two lines.

 -Body ($Line -join "`n")  --  does the same thing.
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Oh wait, just noticed that you're outputting HTML.  One moment.
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
Make sure you're not piping to Out-String in line 1.
Change line 7 to
  -Body ($Line -join "<br>")

Open in new window

0
 
LVL 1

Author Closing Comment

by:jimmylew52
Comment Utility
That worked!!!   Thank You!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

14 Experts available now in Live!

Get 1:1 Help Now