header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

e-mail body from import-csv
Last Post 06 Nov 2008 12:13 AM by khiko. 5 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
the_punisherUser is Offline
New Member
New Member
Posts:7
Avatar

--
26 Sep 2008 03:28 PM  

I'm trying to use code I found to send the contents of a csv file in the body of the e-mail but am having difficulties.  Currently this code produces an e-mail with no message in the body.  How do I put the content of the file into the body of the e-mail.  (I don't want to send the file as an attachment).

 

 

$results = Import-Csv c:\drives.txt -header DriveName,HostName,DriveStatus

#* Create new .NET object and assign to variable
$mail = New-Object System.Net.Mail.MailMessage

#* Sender Address
$mail.From = "from@domain.com";

#* Recipient Address
$mail.To.Add("to@domain.com");

#* Message Subject
$mail.Subject = "Drive Status";

#* Message Body
$mail.Body = $results;

#* Connect to your mail server
$smtp = New-Object System.Net.Mail.SmtpClient("mailserver.domain.com");

#* Uncomment line below if authentication is required
#* $smtp.Credentials = New-Object System.Net.NetworkCredential("username", "passwd");

#* Send Email
$smtp.Send($mail);

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
26 Sep 2008 03:31 PM  
Try to pipe results to out-string:

$mail.Body = ($results | out-string)
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
the_punisherUser is Offline
New Member
New Member
Posts:7
Avatar

--
26 Sep 2008 03:34 PM  

Thanks Shay that worked exactly the way I wanted to to!

khikoUser is Offline
New Member
New Member
Posts:12
Avatar

--
04 Nov 2008 01:04 AM  
Hi Punisher and Shay

how do you make it so that the output on the body of the email does not wrap? or it does not ommit other columns?

hope that question make sense.

i tried using format-table but the last columns are omitted
the_punisherUser is Offline
New Member
New Member
Posts:7
Avatar

--
04 Nov 2008 02:29 PM  

The format of my data has three columns and a carriage return at the end of every row.  It's format is such that I can just leave it alone.

khikoUser is Offline
New Member
New Member
Posts:12
Avatar

--
06 Nov 2008 12:13 AM  
Hi Punisher,

Yes your formatting is as is. So if you have a long name of text in there, you cant really view all of it right?

anyway, i used convertto-html instead and used get-content (instead of import-csv) to send the email and it works perfectly where in i can add more columns and it does show them all in the email.

cheers
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer