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!
-Body ($body -join "`r`n")?rn") 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.