Avatar of cwhitmore88
cwhitmore88
 asked on

How to make log contents display with nice formatting in email using powershell?

I'm using the following command to email a backup log file (backup using Amazon AWS ec2 cli sync):

Send-MailMessage -From "helpdesk@myco.com" -to "myemail@myco.com" -Subject "Backup Log for helpdesk on $date" -Body (Get-Content $log) -Smtpserver "mailserver.com" -port "25" -attachment $log

Open in new window


The email is pretty basic. It displays in basic text format. I'd like the results of this backup to display in HTML with a header saying something like "Helpdesk backup results for $date" and then the results below that.
PowershellAWS

Avatar of undefined
Last Comment
Dan Craciun

8/22/2022 - Mon
Dan Craciun

Have you tried inserting HTML formatting?

$header = "<html><head></head><body><h1>Helpdesk backup results for $date</h1><pre>"
$content = (Get-Content $log)
$footer = "</pre></body></html>"
$body = "$header $content $footer"
Send-MailMessage -From "helpdesk@myco.com" -to "myemail@myco.com" -Subject "Backup Log for helpdesk on $date" -Body $body -Smtpserver "mailserver.com" -port "25" -attachment $log

Open in new window

HTH,
Dan
cwhitmore88

ASKER
Dan,
When I used your code this is what was in the body of the message:

<html><head></head><body><h1>Helpdesk backup results for 2015-09-29</h1><pre> upload: .\Backup.bat to s3://mybucket/daily/batch/Backup.bat Completed 1 of 2 part(s) with 1 file(s) remaining upload: .\Email.ps1 to s3://mybucket/daily/batch/Email.ps1 </pre></body></html>
ASKER CERTIFIED SOLUTION
Dan Craciun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
cwhitmore88

ASKER
Ah yea. that works!

One more formatting question. How can I change the font for the header and put background color behind the header and log entries? (maybe alternating colors between each log entry line to make it easier to read?)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Dan Craciun

You'll need to define a table and use css for that.
I don't know if the limited HTML interpreter used in email messages supports the :nth-child pseudoclass for alternating colors.

You're better off asking this as a new question with a css tag and provide the expected output from your log.