MVP - Most Valuable Professional
rss

Just can't get enough of IT

This blog is about mostly anything in IT. But the primary focuses are Microsoft Technologies like Exchange, Office 365, Azure and Cloud Security.

Inline CSS with ConvertTo-Html

On April 22, 2016
0 Comment
4716 Views

The PowerShell script Get-Diskspace.ps1 uses a pretty basic inline CSS approach to generate a more nicely html email from a PowerShell output.

You can use the following description for your own PowerShell script.

First a new html block is configured and the inline CSS is embedded into the head tag.

# Some CSS to get a pretty report
# Variable containing the inline CSS and the full html head tag
$head = @'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html><head><title>$($ReportTitle)</title>
<style type=”text/css”>
<!–
body {
    font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
}
h2{ clear: both; font-size: 100%;color:#354B5E; }
h3{
    clear: both;
    font-size: 75%;
    margin-left: 20px;
    margin-top: 30px;
    color:#475F77;
}
table{
    border-collapse: collapse;
    border: none;
    font: 10pt Verdana, Geneva, Arial, Helvetica, sans-serif;
    color: black;
    margin-bottom: 10px;
}
 
table td{
    font-size: 12px;
    padding-left: 0px;
    padding-right: 20px;
    text-align: left;
}
 
table th {
    font-size: 12px;
    font-weight: bold;
    padding-left: 0px;
    padding-right: 20px;
    text-align: left;
}
->
</style>
'@

The recurring data content is added to a global varibale using the -Fragment attribute. This ensures that no full html document data is being created.

$global:Html += $wmi | ConvertTo-Html -Fragment -PreContent "<h2>Server $($ServerName)</h2>"

Before sending the html result the full body html is generated by combinding the html fragments and the manually defined html head.

[string]$Body = ConvertTo-Html -Body $global:Html -Title "Status" -Head $head

Send-Mail -From $MailFrom -To $MailTo -SmtpServer $MailServer -MessageBody $Body -Subject $ReportTitle  

Enjoy.



Comments are closed.

Showing 0 Comment