none
Powershell | Email HTML Report RRS feed

  • Question

  • Hi

    I have this script to look for event logs and then output to HTML report. 

    This works fine, no problems, see the report below.EventReport

    But when I do a send-mail the report looks like this on email:

    Email

    This is the script - what am I doing wrong?

    Remove-Variable * -ErrorAction SilentlyContinue
    $time = Get-Date -format "dd-MMM-yyyy"
    $ServerList = get-content -path "U:\Scripts\EventLog Watcher\servers.txt"
    $style = @"
    <!DOCTYPE html>
    <html>
    <head>
    <title>Events Report</title>
    <style>
    /*Reset CSS*/
    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td,
    article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, 
    time, mark, audio, video {margin: 0;padding: 0;border: 0;font-size: 100%;font: inherit;vertical-align: baseline;}
    ol, ul {list-style: none;}
    blockquote, q {quotes: none;}
    blockquote:before, blockquote:after,
    q:before, q:after {content: '';content: none;}
    table {border-collapse: collapse;border-spacing: 0;}
    /*Reset CSS*/
    
    body{
        width:100%;
        min-width:1024px;
        font-family: Verdana, sans-serif;
        font-size:14px;
        /*font-weight:300;*/
        line-height:1.5;
        color:#222222;
        background-color:#fcfcfc;
    }
    
    p{
        color:222222;
    }
    
    strong{
        font-weight:600;
    }
    
    h1{
        font-size:30px;
        font-weight:300;
    }
    
    h2{
        font-size:20px;
        font-weight:300;
    }
    
    #ReportBody{
        width:95%;
        height:500;
        /*border: 1px solid;*/
        margin: 0 auto;
    }
    
    .Overview{
        width:100%;
    	min-width:1280px;
        margin-bottom:30px;
       
    }
    
    .OverviewFrame{
        background:#F9F9F9;
        border: 1px solid #CCCCCC;
    }
    
    table#Overview-Table{
        width:100%;
        border: 0px solid #CCCCCC;
        background:#F9F9F9;
        margin-top:0px;
    }
    
    table#Overview-Table td {
        padding:0px;
        border: 0px solid #CCCCCC;
        text-align:center;
        vertical-align:middle;
    }
    
    table{
        width:100%;
        min-width:1280px;
        /*table-layout: fixed;*/
        /*border-collapse: collapse;*/
        border: 1px solid #CCCCCC;
        /*margin-bottom:15px;*/
    }
    
    /*Row*/
    tr{
        font-size: 12px;
    }
    
    /*Column*/
    td {
        padding:10px 8px 10px 8px;
        font-size: 12px;
        border: 1px solid #CCCCCC;
        text-align:center;
        vertical-align:middle;
    }
    
    /*Table Heading*/
    th {
        background: #f3f3f3;
        border: 1px solid #CCCCCC;
        font-size: 20px;
        font-weight:normal;
        padding:12px;
        text-align:Middle;
        vertical-align:middle;
        overflow: hidden;
    }
    </style>
    </head>
    <body>
    <br><br>
    <center><h1>Events Report</h1></center>
    <center><font face=""Verdana,sans-serif"" size=""3"" color=""#222222"">Generated on $($Date) at $($Time)</font></center>
    
    
    <br>
    <div id=""ReportBody""><!--Start ReportBody-->
    </style>
    "@
    
    
    $starttime = (get-date).adddays(-1)
    $b = foreach ($Server in $ServerList)
    {
    Get-WinEvent -computer $Server -FilterHashtable @{logname=”System”; starttime=$starttime;Level=  2; ID = 36888 } -ErrorAction SilentlyContinue
    }
    $b | ConvertTo-HTML -Head $style MachineName,ID,TimeCreated,ProviderName,LevelDisplayName,Message >D:\Temp\BackupStatus_$time.html
    Thanks, M


    Maelito


    • Edited by Maelito Tuesday, July 19, 2016 3:09 PM Title edit
    Tuesday, July 19, 2016 3:08 PM

Answers

All replies

  • Conversion needs to look like this:

    $htmlBody=$b | Select MachineName,ID,TimeCreated,ProviderName,LevelDisplayName,Message |
         ConvertTo-HTML -Head $style | Out-String
    
    
    
    


    \_(ツ)_/

    Tuesday, July 19, 2016 4:07 PM
    Moderator
  • As usual... you are right! :)

    This is the full script without the Send-Mail part as I am testing on my home laptop.

    Remove-Variable * -ErrorAction SilentlyContinue
    $time = Get-Date -format "dd-MMM-yyyy"
    $ServerList = get-content -path "C:\temp\servers.txt"
    $style = @"
    <!DOCTYPE html>
    <html>
    <head>
    <title>Events Report</title>
    <style>
    /*Reset CSS*/
    html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td,
    article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, 
    time, mark, audio, video {margin: 0;padding: 0;border: 0;font-size: 100%;font: inherit;vertical-align: baseline;}
    ol, ul {list-style: none;}
    blockquote, q {quotes: none;}
    blockquote:before, blockquote:after,
    q:before, q:after {content: '';content: none;}
    table {border-collapse: collapse;border-spacing: 0;}
    /*Reset CSS*/
    
    body{
        width:100%;
        min-width:1024px;
        font-family: Verdana, sans-serif;
        font-size:14px;
        /*font-weight:300;*/
        line-height:1.5;
        color:#222222;
        background-color:#fcfcfc;
    }
    
    p{
        color:222222;
    }
    
    strong{
        font-weight:600;
    }
    
    h1{
        font-size:30px;
        font-weight:300;
    }
    
    h2{
        font-size:20px;
        font-weight:300;
    }
    
    #ReportBody{
        width:95%;
        height:500;
        /*border: 1px solid;*/
        margin: 0 auto;
    }
    
    .Overview{
        width:100%;
    	min-width:1280px;
        margin-bottom:30px;
       
    }
    
    .OverviewFrame{
        background:#F9F9F9;
        border: 1px solid #CCCCCC;
    }
    
    table#Overview-Table{
        width:100%;
        border: 0px solid #CCCCCC;
        background:#F9F9F9;
        margin-top:0px;
    }
    
    table#Overview-Table td {
        padding:0px;
        border: 0px solid #CCCCCC;
        text-align:center;
        vertical-align:middle;
    }
    
    table{
        width:100%;
        min-width:1280px;
        /*table-layout: fixed;*/
        /*border-collapse: collapse;*/
        border: 1px solid #CCCCCC;
        /*margin-bottom:15px;*/
    }
    
    /*Row*/
    tr{
        font-size: 12px;
    }
    
    /*Column*/
    td {
        padding:10px 8px 10px 8px;
        font-size: 12px;
        border: 1px solid #CCCCCC;
        text-align:center;
        vertical-align:middle;
    }
    
    /*Table Heading*/
    th {
        background: #f3f3f3;
        border: 1px solid #CCCCCC;
        font-size: 20px;
        font-weight:normal;
        padding:12px;
        text-align:Middle;
        vertical-align:middle;
        overflow: hidden;
    }
    </style>
    </head>
    <body>
    <br><br>
    <center><h1>Events Report</h1></center>
    <center><font face=""Verdana,sans-serif"" size=""3"" color=""#222222"">Generated on $($Date) at $($Time)</font></center>
    
    
    <br>
    <div id=""ReportBody""><!--Start ReportBody-->
    </style>
    "@
    
    
    $starttime = (get-date).adddays(-1)
    $b = foreach ($Server in $ServerList)
    {
    Get-WinEvent -computer $Server -FilterHashtable @{logname=”System”; starttime=$starttime;Level=  2 } -ErrorAction SilentlyContinue
    }
    $htmlBody=$b | Select MachineName,ID,TimeCreated,ProviderName,LevelDisplayName,Message |
         ConvertTo-HTML -Head $style | Out-String
    
    $colorTagTable = @{Error = ' bgcolor="#ff0000">Error<'
                       }
    $colorTagTable.Keys | foreach { $htmlBody = $htmlBody -replace ">$_<",($colorTagTable.$_) }
         
    $htmlBody | Out-File "C:\Temp\BackupStatus.html"

    I have set the level to be in RED fill colour... :)

    When I am back in the office I will test with Send-Mail.

    Many thanks again, M


    Maelito

    Tuesday, July 19, 2016 10:23 PM
  • Better to simplify.

    To easily dynamically color HTML reports look at these posts: http://tech-comments.blogspot.com/2012/07/powershell-dynamically-color-posh.html

    There are many more posts in the blog showing how to gain easy and deep control of the output of ConvertTo-Html.

    There are other approaches but I think my array is more flexible and can lead to greater understanding of how the big guys downtown do these "spammy" email that catch our attention.

    Use Send-MailMessage.


    \_(ツ)_/

    Tuesday, July 19, 2016 11:02 PM
    Moderator
  • Hi Maelito,

    >>But when I do a send-mail the report looks like this on email:

    A little supplement:

    when using send-mailmessage you could refer to link below:

    Send-MailMessage -From xxx@microst.com -To yyy@microsoft.com -Subject 'test' -BodyAsHtml (get-content c:\xx.html | out-string) -SmtpServer test.contoso.com

    Best regards,


    Andy_Pan

    Wednesday, July 20, 2016 8:15 AM
    Moderator