Skip to content

Instantly share code, notes, and snippets.

@bryanvine
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save bryanvine/393c271d3ecc6f6aae0a to your computer and use it in GitHub Desktop.

Select an option

Save bryanvine/393c271d3ecc6f6aae0a to your computer and use it in GitHub Desktop.
ConvertTo-HtmlTabel - Make Pretty HTML formatted tables
#Requires -Version 3.0
Function ConvertTo-HtmlTable{
<#
.SYNOPSIS
Converts table objects into HTML text for web page conversion and/or emailing as html reports.
.DESCRIPTION
Will convert any object array into a formatted HTML string output for saving as a web page or for emailing.
Doesn't handle nested objects, please only provide a two-demensional table as input.
.PARAMETER Table
Input object(s) to be converted to a formatted HTML table
.PARAMETER BorderColor
Table border color in simple names, default is "black".
.PARAMETER BackgroundColor
Table background color in hex RGB, default is "DFDFDF".
.PARAMETER FontColor
Text font color in hex RGB, default is "000000" (black)
.PARAMETER BoarderWidth
Table boarder width in pixels, default is 1.
.PARAMETER PaddingWidth
Table boarder padding width in pixels, the distance the boarder is from the text, default is 5.
.EXAMPLE
Get-Process | ConvertTo-HtmlTable | Set-Content .\process_report.html
Makes an HTML file from the current process list.
.EXAMPLE
Send-MailMessage -Body (ConvertTo-HtmlTable -Table (Get-Process)) -BodyAsHtml -To $to -From $from -Subject "Test" -smtpServer "exchangerelay1"
Sends an email message with a nicely formatted table in the body of the email.
.LINK
http://www.bryanvine.com/2015/06/powershell-script-convertto-htmltable.html
.LINK
ConvertTo-Html
.NOTES
Author: Bryan Vine
Last updated: 6/24/2015
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,valuefrompipeline=$true,Position=0)]
[Object[]]$Table,
[string]$BorderColor = "black",
[string]$BackgroundColor = "DFDFDF",
[string]$FontColor = "000000",
[int]$BorderWidth = 1,
[int]$PaddingWidth = 5
)
BEGIN{
$AllTabels = @()
$textoutput = "<font color=#$FontColor>"
$head = "<style>"
$head += "TABLE{border-width: $($BorderWidth)px;border-style: solid;border-color:$BorderColor;}"
$head += "Table{background-color:#$BackgroundColor;border-collapse: collapse;}"
$head += "TH{border-width:$($BorderWidth)px;padding:$($PaddingWidth)px;border-style:solid;border-color:$BorderColor;}"
$head += "TD{border-width:$($BorderWidth)px;padding:$($PaddingWidth)px;border-style:solid;border-color:$BorderColor;}"
$head += "</style>"
}
PROCESS{
$AllTabels += $table
}
END{
$textoutput += $AllTabels | ConvertTo-Html -Head $head
$textoutput += "</font>"
Write-Output $textoutput
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment