Adding style

HTML content can be enhanced by adding a Cascading Style Sheet (CSS) fragment. When CSS is embedded in an HTML document, it is added between style tags in the head element.

The following style uses CSS to change the font, color the table headers, define the table borders, and justify the table content:

$css = @' 
<style>
    body { font-family: Arial; } 
    table { 
        width: 100%; 
        border-collapse: collapse; 
    } 
    table, th, td { 
        border: 1px solid Black; 
        padding: 5px; 
    } 
    th { 
        text-align: left; 
        background-color: LightBlue; 
    } 
    tr:nth-child(even) { 
        background-color: GainsBoro; 
    }
</style> 
'@ 

The Head parameter of ConvertTo-Html is used to add the element to the document:

Get-Process |
    ConvertTo-Html -Property Name, Id, WorkingSet -Head $css | 
    Set-Content report.html 

The CSS language is complex and very capable. The elements that are used in the preceding code, and many more, are documented with examples on the W3schools website: https://www.w3schools.com/css/.

Different browsers support different parts of the CSS language, and email clients tend to support a smaller set still. Testing in the expected client is an important part of developing content.

ConvertTo-Html and Send-MailMessage

ConvertTo-Html outputs an array of strings, while Send-MailMessage will only accept a body as a string. Attempting to use the output from ConvertTo-Html with Send-MailMessage directly will raise an error.

The Out-String command may be added to ensure the output from ConvertTo-Html is a string:

$messageBody = Get-Process |
    ConvertTo-Html Name, Id, WorkingSet -Head $css |
    Out-String
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.20.56