Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell : sending e-mails : "newline" problem

Posted on 2009-05-19
2
Medium Priority
?
4,051 Views
Last Modified: 2012-05-07
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
if ($args[0]) {
	$file = $args[0]
# todo : test-path $file
 
 
	$emailFrom = "bla@bla.com"
	$emailTo = "some@one.fr"
	$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

0
Comment
Question by:mchkorg
2 Comments
 
LVL 4

Accepted Solution

by:
peter_field earned 2000 total points
ID: 24448866
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:

$body = Get-Content $file | %{ "$_`r" }

Open in new window

0
 
LVL 7

Author Closing Comment

by:mchkorg
ID: 31582962
Great!
It took me some times to understand it was all about \n and \r as my log files are correct with notepad - maybe windows server's notepad is different from XP's
I didn't check in hexa

Regards
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question