Link to home
Start Free TrialLog in
Avatar of rakkad
rakkadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Powershell formatting using send-mailmessage

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
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"
David, you can mix plain text and HTML, it just doesn't look that good.
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
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.
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.