0

I am searching for a string in multiple files within a folder. If I get that string, then I would like to email all these folders which have this string. I have figured all that out and have adjusted the way I want my format.

The issue is when I send the email, the content shows up all in 1 line. I want this text to be broken word by word. Below is the code:

$find = Get-Childitem –Path C:\inetpub\wwwroot\*\Web.config | Select-String -Pattern "abc..config" 
$boards = $find.Path | Out-String 

[array]$split = $boards.Split("\") 
$count = $split.Count

$out = for ($i=0; $i -le $count; $i++) {
    $split[$i+3]
    $i = $i + 3
    }

$out2 = $out | Out-String

Send-mailmessage -from "" -to "" -subject "Test" -body $out2 -BodyAsHtml -priority  High -dno onFailure -smtpServer 

The issue is that the body shows up like below: ABC ABC2

But, I want the body to be:

ABC
ABC2 

I have also tried getting the output to a text file and using -Raw with gci:

$find = Get-Childitem –Path C:\inetpub\wwwroot\*\Web.config | Select-String -Pattern "abc..config" 
$boards = $find.Path | Out-String 

[array]$split = $boards.Split("\") 
$count = $split.Count

$out = for ($i=0; $i -le $count; $i++) {
    $split[$i+3]
    $i = $i + 3
    }

$out | Out-file -FilePath C:\Test.txt 
$body = Get-content C:\Test.txt -Raw 


Send-mailmessage -from "" -to "" -subject "Test" -body $body -BodyAsHtml -priority  High -dno onFailure -smtpServer 

I did use Out-String and -Raw with gci and I am on V4. Not sure what I am missing. When I output the file, it is correct format with line breaks but not when I email. I would prefer not creating a text document and just use the variable in the body.

Thanks

0

The content of your message body is NOT HTML, yet you are using -bodyAsHtml on send-mailmessage. Your body is just text, and when you specify -bodyAsHTML that causes the line breaks to be removed. Removing -bodyAsHtml should fix your problem.

Within an HTML document, line breaks are represented by <P>, <BR>, and are only preserved when they appear inside of <PRE>.

  • That worked! Thank you much! I pretty much ignored it. – nav Sep 22 '17 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.