header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Using text file as body of email.
Last Post 14 Dec 2008 11:10 AM by Dan Ball. 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 03: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?

    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    13 Dec 2008 11:39 PM
    pipe the file content to out-string:

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

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    14 Dec 2008 11:00 AM
    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
    Dan BallUser is Offline
    Basic Member
    Basic Member
    Posts:154
    Avatar

    --
    14 Dec 2008 11:10 AM
    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.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer