Formatting text


This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 3 months, 1 week ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #44449
    Profile photo of bvi1998 .
    bvi1998 .


    I feel I have tried every which way to set the body of an email with the contents of two files, but not getting the expected result.

    I retrieve the content of each file and set the content of this data to a file called body.txt:

    get-content "C:\scripts\output\ $(get-date -f "MM_dd_yyyy")_US.txt" | Add-Content c:\scripts\output\body.txt
    get-content "C:\scripts\output\ $(get-date -f "MM_dd_yyyy")_MEX.txt" | Add-content c:\scripts\output\body.txt

    The file body.txt, if I open it in notepad, clearly shows CRLF at the end of each line. I then set the variable $body to a string by doing the following:

    $body = (get-content -path c:\scripts\output\body.txt) | out-string

    When I then try to email using send-mailmessage -body $body .... the body of the email contains all of my lines in one long string. I know the out-string does this, but the send-mailmessage requires the info to be a string.

    I will bet I am doing WAY too much manipulation of the data 🙂 ...

    Would someone please give me a suggestion?

    Profile photo of Wes Stahler
    Wes Stahler

    This works fine for me as long as the "-BodyAsHtml" parameter is not included with Send-MailMessage. If HTML is required, then some extra formatting steps may be needed.

    Profile photo of bvi1998 .
    bvi1998 .

    Sorry I was not clear – the lines all come out in one string but I'd like them to appear in the body of the message as the original lines with CRLF for easy reading.

    Profile photo of Rob Simmers
    Rob Simmers

    The body parameter requires a string value. Get-Content, by default, returs an Array of lines. These two methods worked for me:

    Adding the -Raw switch:

    $body = Get-Content C:\Users\Rob\Desktop\test.txt -Raw

    or using FileSystemObject, vbScript ole skool style:

    $body = (New-Object -Com Scripting.FileSystemObject).OpenTextFile("C:\Users\Rob\Desktop\test.txt",1).ReadAll()
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.