1

I apologize for this in advance because I see plenty of questions regarding it throughout the web but for some reason I'm still having issues.

I have a script that creates an array that has information dynamically added to it. When the script completes, I need it to email that information to me. The problem is that each line is combined to that I get 1 line. For example:

$Body = @()
$Body += "1"
$Body += "2"
$Body += "3"
$Body += "4"

Here's my send command:

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

What I get in the email body is: 1234

I've tried this for loop to append `n to the beginning of each line (except the 1st) like so:

for ($i = 0; $i -lt Body.Count; $i++){
    if($i -eq 0){
        Write-Host $i
    }else{
        $Body[$i] = "`n" + $Body[$i]
        Write-Host $Body[$i]
    }
}

The results of that are better but I get an extra line:

1

2

3

4

Ultimately I just want this:

1
2
3
4

In the past, I've gotten the format I want by creating the variable like this:

$Body = @"

This is the email I want to send.  It formats great if:
    1) I want to make all the content static.

Is it possible to create the `$Body variable like this but add line by line dynamically and maintain a serpate line. (without extra lines)?

"@  

What am I missing? It HAS to be something simple... Thanks for your help!

BBL Admin
  • 57
  • 1
  • 2
  • 9
  • Perhaps ``-Body ($body -join "`r`n")``? – Ryan Bemrose May 24 '16 at 20:02
  • Actually, I suspect this is something in your e-mail chain "helpfully" combining line breaks. Outlook does this by default, and will only do it if there is no indentation and only a single newline between lines. Your last example wouldn't trigger it, but your "I just want this" example would. – Ryan Bemrose May 24 '16 at 20:05
  • -Body ($body -join "`r`n") works if I setup a demo and run it within a Powershell window but within the context of the script I get everything on one line. Copied and pasted just to be sure... I'll keep investigating this and Ryan's comment. – BBL Admin May 24 '16 at 21:31

1 Answers1

1

WOW so it's such a finicky thing... I found that using this would get everything to look right (as suggested by Ryan, above):

-Body ($Body -Join "`r`n")

But in the context of my actual script it wasn't working. Well, it appears that it all had to do with the way the date was going in there. Here's a reduction to show:

DOES NOT WORK

$Body = @()
$Body += "Beginning processing on: " + (Get-Date)
$Body += "No advertisements found, exiting"

ALSO DOES NOT WORK

$Body = @()
$DateTime = Get-Date
$Body += "Beginning processing on: $DateTime"
$Body += "No advertisements found, exiting"

When I removed anything that had to do with the Date, formatting was correct. Ultimately it just took a simple "." to make it work right.

WORKS - NOTE THE 3RD LINE

$Body = @()
$DateTime = Get-Date
$Body += "Beginning processing on: $DateTime."     #Note the period at the end of the line.
$Body += "No advertisements found, exiting"

Presumably I could just concatenate and it would work but oh well, I'm keeping it as it. I didn't think that it would be that finicky about a period but now I know.

BBL Admin
  • 57
  • 1
  • 2
  • 9
  • It seems like the DateTime class is just really picky about how you cast it to string. See also http://stackoverflow.com/questions/30538994/get-date-cast-to-string-vs-tostring – Ryan Bemrose May 24 '16 at 22:03