The Surly Admin

Father, husband, IT Pro, cancer survivor

ConvertTo-AdvHTML Help

Synopsis
Advanced replacement of ConvertTo-HTML cmdlet.

Source Code
ConvertTo-AdvHTML at Spiceworks

Syntax
ConvertTo-AdvHTML [-InputObject] <Object[]> [[-HeadWidth] <String[]>] [[-CSS] <String>] [[-Title] <String>] [[-PreContent] <String>] [[-PostContent] <String>] [[-Body] <String>] [-Fragment ] [<CommonParameters>]

Description
This function allows for vastly greater control over cells and rows in a HTML table. It takes ConvertTo-HTML to a whole new level! You can now specify what color a cell or row is (either dirctly or through the use of CSS). You can add links, pictures and pictures AS links.  You can also specify a cell to be a bar graph where you control the colors of the graph and text that can be included in the graph.

All color functions are through the use of imbedded text tags inside the properties of the object you pass to this function. It is important to note that this function does not do any processing for you, you must make sure all control tags are already present in the object before passing it to the function.

Here are the different tags available:

Syntax Comment
[cell:<color>]<optional text> Designate the color of the cell. Must be at the beginning of the string.Example:   [cell:red]System Down
[row:<color>] Designate the color of the row. This control can be anywhere, in any property of the object.Example:   [row:orchid
[cellclass:<class>]<optional text> Designate the color, and other properties, of the
cell based on a class in your CSS. You must have the class in your CSS (use the -CSS parameter). Must be at the beginning of the string.Example:   [cellclass:highlight]10mb
[rowclass:<class>] Designate the color, and other properties, of the row based on a class in your CSS. You must have the class in your CSS (use the -CSS parameter). This control can be anywhere, in any property of the object.Example:   [rowclass:greyishbold]
[image:<height;width;url>]<alternate text> Include an image in your cell. Put size of picture in pixels and url seperated by semi-colons. Format must be height;width;url. You can also include other text in the cell, but the [image] tag must be at the end of the tag (so the alternate text is last).Example:   [image:100;200;htt_://www.sampleurl.com/sampleimage.jpg]Alt Text For Image
[link:<url>]<link text> Include a link in your cell. Other text is allowed in the string, but the [link] tag must be at the end of the string.Example:   blah blah blah [link:www.thesurlyadmin.com]Cool PowerShell Link
[linkpic:<height;width;url to pic>]<url for link> This tag uses a picture which you can click on and go to the specified link. You must specify the size of the picture and url where it is located, this information is separated by semi-colons. Other text is allowed in the string, but the [link] tag must be at the end of the string.Example:   [linkpic:100;200;htt_://www.sampleurl.com/sampleimage.jpg]www.thesurlyadmin.com
[bar:<percent;bar color;remainder color>]<optional text> Bar graph makes a simple colored bar graph within the cell. The length of the bar is controlled using <percent>. You can designate the color of the bar, and the color of the remainder section. Due to the mysteries of HTML, you must designate a width for the column with the [bar] tag using the HeadWidth parameter.So if you had a percentage of 95, say 95% used disk you would want to highlight the remainder for your report:Example:   [bar:95;dark green;red]5% freeWhat if you were at 30% of a sales goal with only 2 weeks left in the quarter, you would want to highlight that you have a problem.Example:   [bar:30;darkred;red]30% of goal

Parameters

-InputObject <Object[]>

The object you want converted to an HTML table
Required? true
Postiion 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false

-HeadWidth <String[]>

You can specify the width of a cell. Cell widths are in pixels and are passed to the parameter in array format. Each element in the array corresponds to the column in your table, any element that is set to 0 will designate the column with be dynamic. If you had four elements in your InputObject and wanted to make the 4th a fixed width–this is required for using the [bar] tag–of 600 pixels:-HeadWidth 0,0,0,600
Required? false
Postiion 2
Default value
Accept pipeline input? false
Accept wildcard characters? false

-CSS <String>

Designate custom CSS for your HTML
Required? false
Postiion 3
Default value <style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;font-size:120%;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
Accept pipeline input? false
Accept wildcard characters? false

-Title <String>

Specifies a title for the HTML file, that is, the text that appears between the <TITLE> tags.
Required? false
Postiion 4
Default value
Accept pipeline input? false
Accept wildcard characters? false

-PreContent <String>

Specifies text to add before the opening <TABLE> tag. By default, there is no text in that position.
Required? false
Postiion 5
Default value
Accept pipeline input? false
Accept wildcard characters? false

-PostContent <String>

Specifies text to add after the closing </TABLE> tag. By default, there is no text in that position.
Required? false
Postiion 6
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Body <String>

Specifies the text to add after the opening <BODY> tag. By default, there is no text in that position.
Required? false
Postiion 7
Default value
Accept pipeline input? false
Accept wildcard characters? false

-Fragment <SwitchParameter>

Generates only an HTML table. The HTML, HEAD, TITLE, and BODY tags are omitted.
Required? false
Postiion named
Default value false
Accept pipeline input? false
Accept wildcard characters? false

Inputs
System.Management.Automation.PSObject
You can pipe any .NET object to ConvertTo-AdvHtml.

Outputs
System.String
ConvertTo-AdvHtml returns series of strings that comprise valid HTML.

Examples
The following code is an example of an array of objects with the ConvertTo-AdvHTML control tags built into the property values.

$Data= @(
    [PSCustomObject]@{
        Server ="[row:orchid]Server1"
        Description ="Hello1"
        Status ="[cellclass:up]Up"
        Disk ="[bar:45;Purple;Orchid]55% Free"
    },
    [PSCustomObject]@{
        Server ="Server2"
        Description ="Hello2"
        Status ="[cell:green]Up"
        Disk ="[bar:65;DarkGreen;Green]65% Used"
    },
    [PSCustomObject]@{
        Server ="Server3"
        Description ="Goodbye3"
        Status ="[cell:red]Down"
        Disk ="[bar:95;DarkGreen;DarkRed]5% Free"
    },
    [PSCustomObject]@{
        Server ="Server4"
        Description ="This is quite a cool test"
        Status ="[cell:green]Up"
        Disk ="[image:150;650;https://pughspace.files.wordpress.com/2014/01/test-connection.png]Test Images"
    },
    [PSCustomObject]@{
        Server ="Server5"
        Description ="SurlyAdmin"
        Status ="[cell:red]Down"
        Disk ="[link:http://thesurlyadmin.com]The Surly Admin"
    },
    [PSCustomObject]@{
        Server ="Server6"
        Description ="MoreSurlyAdmin"
        Status ="[cell:purple]Updating"
        Disk ="[linkpic:150;650;https://pughspace.files.wordpress.com/2014/01/test-connection.png]http://thesurlyadmin.com"
    }
)

$HTML=$Data | ConvertTo-AdvHTML -HeadWidth 0,0,0,600 -PreContent "<p><h1>Sample ConvertTo-AdvHTML Report</h1></p><br>" -PostContent "<br>Done! $(Get-Date)" -Title "Cool Test!"

Produces the following table:


Sample ConvertTo-AdvHTML Report

Server Description Status Disk
Server1 Hello1 Up
55% Free
Server2 Hello2 Up
65% Used
Server3 Goodbye3 Down
5% Free
server4 This is quite a cool test Up Test Images
server5 SurlyAdmin Down The Surly Admin
server6 MoreSurlyAdmin Updating

Done! 02/23/2014 17:41:09


Notes

Author: Martin Pugh
Twitter: @thesurlyadm1n
Spiceworks: Martin9700
Blog: www.thesurlyadmin.com

Changelog:

1.0 Initial Release
Advertisements

2 Comments

  1. […] As I said, instead of going into a long, dry explanation of how the script works let’s instead write a simple disk information script that uses the function.  First things first, get the advanced function from here.  Then read the help file here. […]

    Pingback by ConvertTo-AdvHTML – New Advanced Function for HTML Reporting « The Surly Admin | February 27, 2014

  2. […] script up with error checking, verbose output, that kind of thing.  I also decided to leverage my ConvertTo-AdvHTML function so as domains get closer to their expiration date the color of the row would change […]

    Pingback by WhoIS Report « The Surly Admin | March 7, 2014


%d bloggers like this: