Link to home
Create AccountLog in
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.
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

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
Avatar of cwhitmore88
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
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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?)
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.