SimonS SimonS - 3 months ago 11
PowerShell Question

E-Mail body new line for each string

I have an array called

$faultypath
which contains filepath-strings.

To send them via
Send-Mailmessage
I have to merge them into a single string:

$bdy = $faultypath | Out-String


I send my E-Mail as HTML (
-BodyAsHtml
) is there a trick I can use to get every filepath in my
$bdy
on a new line? Right now they're on the same line in the E-Mail.

if($faultypath) {
$utf8 = New-Object System.Text.utf8encoding
$bdy = $faultypath | Out-String
Send-MailMessage -From gdfgdfg `
-To dgdfgfdg `
-SmtpServer dgfgdfg `
-Subject "LogDeleter: Log-Pfad nicht gefunden" `
-Body "LogDeleter.ps1 konnte folgende Pfade nicht überprüfen:<br /><br />$bdy<br /><br />bitte aus LogDelete.ps1 löschen." `
-BodyAsHtml `
-Encoding $utf8
}

Answer

Use -join to combine the paths with a <br />. Then you have each path separated by a new line and don't need the Out-String anymore:

$bdy = $faultypath -join '<br />'