Avatar of rakkad
Flag 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



Avatar of undefined
Last Comment
David Johnson, CD

8/22/2022 - Mon

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
David Johnson, CD

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.
David Johnson, CD

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy

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.
David Johnson, CD

Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.