We help IT Professionals succeed at work.

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

cwhitmore88
cwhitmore88 asked
on
116 Views
Last Modified: 2015-09-29
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.
Comment
Watch Question

Dan CraciunIT Consultant
CERTIFIED EXPERT

Commented:
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

Author

Commented:
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>
IT Consultant
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
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?)
Dan CraciunIT Consultant
CERTIFIED EXPERT

Commented:
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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.