Solved

Get line breaks in powershell output to email

Posted on 2013-10-23
5
5,142 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
ID: 39594326
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
ID: 39594385
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
ID: 39594457
Oh wait, just noticed that you're outputting HTML.  One moment.
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 39594493
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
ID: 39594646
That worked!!!   Thank You!!!
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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

806 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