0

I am trying to output a log file to a variable and use that variable as Body on a Send-MailMessage. The problem is that all CRLFs in the variable are missing in the output.

ex.

$Body = get-content .\TTT.txt
$body
Test
Test1
Test2

write-host "$($Body)"
Test Test1 Test2

Is there a way to avoid it? (keep the CRLF)

tfonias74
  • 136
  • 9

3 Answers3

3

Documentation gives an answer:

When writing a collection to the host, elements of the collection are printed on the same line separated by a single space. This can be overridden with the Separator parameter.

Write-Host docs

Avshalom
  • 8,657
  • 1
  • 25
  • 43
metablaster
  • 1,958
  • 12
  • 26
3

If you must use Write-Host then add the parameter -Separator with Newline:

Write-Host $Body -Separator "`n"
Avshalom
  • 8,657
  • 1
  • 25
  • 43
  • Ok, thanks. It works for the write-host. In the case of using it as a body in the Send-MailMessage command, is there a way to do the same? – tfonias74 Dec 14 '22 at 10:38
  • depend on the request body requirement, you can convert it to JSON in most cases – Avshalom Dec 14 '22 at 10:40
0

From what I saw at Cannot Get Send-MailMessage to Send Multiple Lines the workaround is like this:

$Body = @()
$Body += "Test"
$Body += "Test1"
$Body += "Test2"

etc

Send-MailMessage -To xxx -From xxx -Smtp xxx -Subject "Test" -Body ($Body | Out-String)

The above works as I wanted.

tfonias74
  • 136
  • 9