Powershell using string and file content in email message body without breaking formatting -


i have powershell script has part needs email information. have requirement include 2 pieces of information inside message body, having issues formatting. first part line of text store inside powershell script variable, , second part information text file. below simplified version of want do:

$info1="the following fruits ready eat:   " $info2=get-content "info2.txt" $body=$info1 + $info2 $body=$body | out-string send-mailmessage -to myname@mydomain.com -subject "fruit status" -from  fruit@mydomain.com -body $body -smtpserver smtp.mydomain.org -usessl 

the file info2.txt contains following info. 3 columns list of fruits tab separators:

col1        col2        col3 apple       orange      pear banana      grape       strawbery kiwi        peach       apricot 

the above code works email looks this:

the following fruits ready eat:   col1        col2        col3 apple      orange      pear banana     grape        strawbery kiwi      peach       apricot 

it keeps line spaces in $info1 stored variable string, places content of info2.txt file in 1 continuous line, no line breaks or tabbed formatting.

i tested below changes whereby line of text $info1 variable, instead obtained doing get-content on same piece of text inside file named "info1.txt". when append both pieces of information message body, formatting maintained , email looks ok.

$info1=get-content "info1.txt"  $info2=get-content "info2.txt" $body=$info1 + $info2 $body=$body | out-string send-mailmessage -to myname@mydomain.com -subject "fruit status" -from  fruit@mydomain.com -body $body -smtpserver smtp.mydomain.org -usessl    following fruits ready eat:   col1        col2        col3  apple       orange      pear  banana      grape       strawbery  kiwi        peach       apricot* 

i notice if below using first method, grabbing file content first , string variable, formatting kept fine:

$body=$info2 + $info1 

it when start string variable first, mucks formatting in email file content part.

i prefer keep script more compact , minimize writing out files on disk , used stored variables. there way can use method 1 appending string or multiple strings, followed file content formatting kept? not wish use html overly complicate matters text file content working body -body rather -bodyashtml unless way sort this.

i believe have found answer. played get-content -raw argument before think having trouble using content stored in pipeline. when use below directly on file no other clauses, fixes issue:

$info1="the following fruits ready eat:   " $info2=get-content "info2.txt" -raw $body=$info1 + $info2 $body=$body | out-string send-mailmessage -to myname@mydomain.com -subject "fruit status" -from  fruit@mydomain.com -body $body -smtpserver smtp.mydomain.org -usessl 

get-content gets content of item @ location specified, such text in file. default powershell read content 1 line @ time , return collection of system.string objects. carriage returns (cr/lf) stripped original file when powershell displays collection of strings, re-insert carriage returns display original file.

if specify -raw, behaviour reversed , entire file read @ once.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -