Register
: :
Login
Home
Forums
Blogs
Podcast
Directories
Scripts
Downloads
Community
User Group Support
Learning Resources
We have a new sponsor! Introducting
Pragma Systems
. See the home page for details.
Unanswered
Active Topics
Forums
Search
Members
Forums
>
Using PowerShell
>
General PowerShell
Using text file as body of email.
Last Post 14 Dec 2008 07:10 PM by
DanBall
. 3 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
trey85stang
New Member
Posts:1
13 Dec 2008 11:47 PM
Hello all, I have a script that sends an email out and I am getting the content from a text file from another script. Im searching it for a specific word/line and if its there Im emailing the file out.
Everything works.. the only problem is the formatting in the body of the email seems to ignore returns.
i.e. the log looks like this:
"
this is line 1
this is line 3
this is line 5
"
but the body of my email looks like this:
"this is line 1 this is line 3 this is line 5"
Since there could be up to 20-30 lines... it makes it hard to read.. Im just using: "gc my.txt" to put the information into the body of the email.
Does anyone know how to get the email formatted the way I want?
Shay
Veteran Member
Posts:1091
14 Dec 2008 07:39 AM
pipe the file content to out-string:
$msg.body = (gc my.txt | out-string)
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar:
glnsize
Basic Member
Posts:186
14 Dec 2008 07:00 PM
yeah, gc strips off the /r/n line breaks when it reads everything into an array. When you pipe something from get-content it will send one line at a time down the pipeline, but without the line breaks. it's a pain in the butt when parsing log files with regex... IMO there should be a switch to leave them in there.
The reason its stripped out is because Get-Content is meant to be used in conjunction with the format-* and Out-* cmdlets. As Shay already highlighted Out-string being an out-* deserializes the data back into a string, and inserts the appropriate line breaks.
You can also do so manually:
$body = [string]::join([environment]::NewLine, (get-content msgbody.txt))
The above will join every line outputted by gc with a line break. Why would you do this... well in your case you wouldn't 30 lines isn't big enough to bother... out-string will work fine.
Another option is to just embed your text with a here string (at least I think it's called a here string)
$txt = @"
put your text in here...
SYSTEM : $env:computername
USER : $env:username
line breaks and spaces are preserved.
"@
hope that helps
~Glenn
DanBall
Basic Member
Posts:150
14 Dec 2008 07:10 PM
To get my scripts to do that correctly, I append `n at the end of each line.
You are not authorized to post a reply.
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
Forums
>
Using PowerShell
>
General PowerShell
Active Forums 4.1
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008