Question : powershell : sending e-mails : "newline" problem

Hello,

I have a .BAT calling a .PS1 with output redirection to a log file :
powershell -command "& 'C:\script.ps1' " > C:\script.log

The PS1 is just doing some "echo $something" sometimes
The log file is correctly generated

Then my .BAT calls another PS1 script (see below) to e-mail the log file via an Exchange server. (the log file is args0] in the .BAT)
I'm checking the e-mail with OL2007

* When I send the content as an attachment, it's OK but it's boring to have to open the file
* When I send the content inline => all newlines disappear!!! everything is on one line depending on my outlook window size

I don't know if it's related to Outlook or Powershell (or both). I can't find in OL any place to check the real e-mail content (like headers and body)
I can't find any "System.Net.Mail.MailMessage" object property to force some encoding or raw text format.

Can someone help me?
Thank you
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
if ($args[0]) {
	$file = $args[0]
# todo : test-path $file
 
 
	$emailFrom = "[email protected]"
	$emailTo = "[email protected]"
	$subject = "powershell / logs"
 
 
# if I want it as an attached file to see newlines
	#$body = "log file attached"
# if I want it inline
	$body = Get-Content $file
	$smtpServer = "my_smtp"
 
 
	$msg = new-object System.Net.Mail.MailMessage $emailFrom, $emailTo, $subject, $body
 
 
# below When I want the log file attached, instead of inline
	#$attachment = new-object System.Net.Mail.Attachment $fichier
	#$msg.Attachments.Add($attachment)
 
 
	$client = new-object System.Net.Mail.SmtpClient $smtpServer
	$client.Send($msg)
} else { echo "arg missing" }
Open in New Window Select All

Answer : powershell : sending e-mails : "newline" problem

PowerShell only outputs newlines, not a carriage return when it is redirected via CMD.

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/add-content.mspx

Outlook must not be able to handle this (you will find notepad doesn't like this, but wordpad does, so support on Windows is not entirely consistent).

In your PowerShell script that sends the email, try this:
1:
$body = Get-Content $file | %{ "$_`r" }
Open in New Window Select All
Random Solutions  
 
programming4us programming4us