header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Using text file as body of email.
Last Post 14 Dec 2008 07:10 PM by DanBall. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
trey85stangUser is Offline
New Member
New Member
Posts:1
Avatar

--
13 Dec 2008 11:47 PM  
Hello all,  I have a script that sends an email out and I am getting the content from a text file from another script.  Im searching it for a specific word/line and if its there Im emailing the file out.

Everything works.. the only problem is the formatting in the body of the email seems to ignore returns.

i.e. the log looks like this:
"
this is line 1

this is line 3

this is line 5
"

but the body of my email looks like this:

"this is line 1   this is line 3   this is line 5"

Since there could be up to 20-30 lines... it makes it hard to read..  Im just using:  "gc my.txt" to put the information into the body of the email.

Does anyone know how to get the email formatted the way I want?

ShayUser is Offline
Veteran Member
Veteran Member
Posts:1091
Avatar

--
14 Dec 2008 07:39 AM  
pipe the file content to out-string:

$msg.body = (gc my.txt | out-string)

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar:
glnsizeUser is Offline
Basic Member
Basic Member
Posts:186

--
14 Dec 2008 07:00 PM  
yeah, gc strips off the /r/n line breaks when it reads everything into an array. When you pipe something from get-content it will send one line at a time down the pipeline, but without the line breaks. it's a pain in the butt when parsing log files with regex... IMO there should be a switch to leave them in there.

The reason its stripped out is because Get-Content is meant to be used in conjunction with the format-* and Out-* cmdlets. As Shay already highlighted Out-string being an out-* deserializes the data back into a string, and inserts the appropriate line breaks.

You can also do so manually:
$body = [string]::join([environment]::NewLine, (get-content msgbody.txt))

The above will join every line outputted by gc with a line break. Why would you do this... well in your case you wouldn't 30 lines isn't big enough to bother... out-string will work fine. 

Another option is to just embed your text with a here string (at least I think it's called a here string)

$txt = @"
put your text in here...  

   SYSTEM   : $env:computername
   USER         : $env:username
line breaks and spaces are preserved.
"@

hope that helps
~Glenn
DanBallUser is Offline
Basic Member
Basic Member
Posts:150
Avatar

--
14 Dec 2008 07:10 PM  
To get my scripts to do that correctly, I append `n at the end of each line.
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 footer
footer