Powershell formatting using send-mailmessage

rakkad
rakkad used Ask the Experts™
on
I have created a powershell script that uses send-mailmessage and uses get-content to read in a file in html, however,  I want the message body to contain text and use the get-content to get the file so, it should say something like:-

Please see attached file

and then use the get-content to retrieve a html file

Hope this makes sense

Thanks




T
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
You want to have text and file content both in the body, and no attachment?
A simple way is to just put it together - not pretty, but works:
$body = 'Please see attached file<br><br>'+(-join (get-content file.html))

Open in new window

Of course you should make sure to provide the -BodyAsHtml switch with your Send-MailMessage.
Top Expert 2016

Commented:
you can't mix text/html with powershell.  You need to not use get content but simply use the -attachment option in your send-mailmessage i.e.
-Attachments "data.csv"
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
David, you can mix plain text and HTML, it just doesn't look that good.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2016

Commented:
You misunderstood.. you cannot run a powershell script to a mail message and execute it in the mail message.. the body of an email message is text i.e.
start-process cmd.exe in the message will not ever execute
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
The question could be read indeed that way - sending over PowerShell code to get the content of the file. But that does not make much sense, so I went with the more useful interpretation.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
I've doubts about what has been really meant here, so summarizing the available opitions.

Send the text file as attachment is not much different than to send it over as text in the HTML body, but doing the later is better for immediate review. Both options require the complete text to be stored in the local PST or OST, and if Exchange 2013 is used, one single time on the server (with auto dedup), with older Exchanges storing a copy for each receiver.

Sending over a PS script as attachment is possible but requires an inacceptable weakening of Outlook security settings to allow execution. PS scripts in email are already used for attacks.

Having local VBA code in Outlook requires some sophisticated setup, and to allow macro execution (again a security risk, though it can be restricted much better to be "secure"). Then a mail can contain a certain subject, some keywords in the body or whatever, and the file name to display. A button to call the (local) macro, e.g. located in the Quick Access bar, can be used to run the code to display the file.
This would keep the file in a single location, not having it contained in any mail. But the effort is worth it only for very large files, IMHO.
Top Expert 2016

Commented:
answered

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial