PowerShell and HTML Table

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
waynecierkowski
Posts: 20
Joined: Wed May 14, 2014 11:40 am

PowerShell and HTML Table

Post by waynecierkowski » Thu Jun 07, 2018 12:01 pm

I'm in the process of trying to write a backup summary report. But in order to do this I had to figure out HTML Tables and how to populate them. I figured out how to create the table and variables in it to capture the data.

In this sample code I was assigning data to variables for testing purposes, the actual program will be getting the data from various files created from my backup program.

Code: Select all

$tableHeader = "
<table> <tbody>
<td width='150' align='center'><b>Drive/Element</b></td>
<td width='175' align='center'><b>Started</b></td>
<td width='175' align='center'><b>Ended</b></td>
<td width='50' align='center'><b>Images</b></td>
<td width='75' align='center'><b>Files</b></td>
<td width='130' align='center'><b>Kbytes</b></td>
<td width='100' align='center'><b>Status</b></td>
</tr>
"
$DataRow = "
<tr>
<td align='left'>$DriveElement</td>
<td align='center'>$Started</td>
<td align='center'>$Ended</td>
<td align='center'>$Images</td>
<td align='right'>$Files</td>
<td align='right'>$Kbytes</td>
<td align='center'>$Status</td>
</tr>
"
$ServerTotalRow = "
<tr>
<td align='left'><b>*** Server Total ***</b></td>
<td align='center'>$null</td>
<td align='center'>$null</td>
<td align='center'><b>$ServerTotalImages</b></td>
<td align='right'><b>$ServerTotalFiles</b></td>
<td align='right'><b>$ServerTotalKbytes</b></td>
<td align='center'>$null</td>
</tr>
"
Set-Content D:\Test\Test.htm $tableHeader

$ServerImages = 0
$ServerFiles = 0
$ServerKbytes = 0

$DriveElement = " "
$Started = " "
$Ended = " "
$Images = " "
$Kbytes = " "
$Files = " "
$Status = " "

$TempKbytes = "6652774856"
$TempKbytes = $TempKbytes -as [decimal]
$Kbytes = $TempKbytes.tostring('N0')
$ServerKbytes = $ServerKbytes + $TempKbytes
$TempFiles = "5729704"
$TempFiles = $TempFiles -as [decimal]
$Files = $TempFiles.tostring('N0')
$ServerFiles = $ServerFiles + $TempFiles
$DriveElement = "Oracle DB's OPPRD"
$Started = "May 07 2018 - Mon 10:59"
$Ended = "May 07 2018 - Mon 11:04"
$Images = 1
$ServerImages = $ServerImages + $Images
$Status = "Success"
Add-Content D:\Test\Test.htm $DataRow
Write-Host $DataRow

$DriveElement = " "
$Started = " "
$Ended = " "
$Images = " "
$Kbytes = " "
$Files = " "
$Status = " "

$TempKbytes = "6652774856"
$TempKbytes = $TempKbytes -as [decimal]

$Kbytes = $TempKbytes.tostring('N0')
$ServerKbytes = $ServerKbytes + $TempKbytes
$TempFiles = "1521275"
$TempFiles = $TempFiles -as [decimal]
$Files = $TempFiles.tostring('N0')
$ServerFiles = $ServerFiles + $TempFiles
$DriveElement = "Oracle DB's OPPRD"
$Started = "May 07 2018 - Mon 10:59"
$Ended = "May 07 2018 - Mon 11:04"
$Images = 69
$ServerImages = $ServerImages + $Images
$Status = "Success"
Add-Content D:\Test\Test.htm $DataRow
Write-Host $DataRow

$ServerTotalKbytes = $ServerKbytes.tostring('N0')
$ServerTotalFiles = $ServerFiles.tostring('N0')
$ServerTotalImages = $ServerImages.tostring('N0')
Add-Content D:\Test\Test.htm $ServerTotalRow
Write-Host $ServerTotalRow


Now my issue here is if I select the code in the PowerShell Studio app and select "Run Selection in Console" all works great. But if I build a executable and execute that program my htm file has the headings but no data.

Any one have any ideas why this is not working. I'm new to HTML tables and learning each day.

Any suggestions would be greatly appreciated. TIA..

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell and HTML Table

Post by jvierra » Thu Jun 07, 2018 12:38 pm

I recommend using an HTML template file. Replace all variables with string replacement references:

'This {0} is a {1}' -f 'animal','cat'

The file can hold the format. You can also use a true XHTTP file and then easily edit it with the XML object.

Here are some posts that will help you understand how to manage HTML with PowerShell
http://tech-comments.blogspot.com/2012/ ... -posh.html
http://tech-comments.blogspot.com/2012/ ... sh_23.html
http://tech-comments.blogspot.com/2012/ ... -posh.html

http://tech-comments.blogspot.com/search/label/html
http://tech-comments.blogspot.com/2013/ ... art-2.html

Hand carved HTML is always messy and hard to work with. I recommend using the techniques described in the blogs.

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell and HTML Table

Post by jvierra » Thu Jun 07, 2018 12:56 pm

This is how a header looks:

Code: Select all

$tblHeaderTemplate = @'
<table> 
    <thead>
        <tr>
        <th width='150' align='center'><b>Drive/Element</b></th>
        <th width='175' align='center'><b>Started</b></th>
        <th width='175' align='center'><b>Ended</b></th>
        <th width='50' align='center'><b>Images</b></th>
        <th width='75' align='center'><b>Files</b></th>
        <th width='130' align='center'><b>Kbytes</b></th>
        <th width='100' align='center'><b>Status</b></th>
        </tr>
    </thead>
'@

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell and HTML Table

Post by jvierra » Thu Jun 07, 2018 1:01 pm

This is how to build rows from a template:

Code: Select all


$tblRowTemplate = @'
<tr>
    <td align='left'>{0}</td>
    <td align='center'>{1}</td>
    <td align='center'>{2}</td>
    <td align='center'>{3}</td>
    <td align='right'>{4:N0}</td>
    <td align='right'>{5:N0}</td>
    <td align='center'>{6}</td>
</tr>
'@


$Kbytes = 6652774856
$ServerKbytes = $ServerKbytes + $TempKbytes
$TempFiles = 5729704
$ServerFiles = $ServerFiles + $TempFiles
$DriveElement = 'Oracle DB''s OPPRD'
$Started = 'May 07 2018 - Mon 10:59'
$Ended = 'May 07 2018 - Mon 11:04'
$Images = 1
$ServerImages = $ServerImages + $Images
$Status = 'Success'
$row = $tblRowTemplate -f $DriveElement,$Started,$Ended,$Images,$Files,$Kbytes,$Status

User avatar
waynecierkowski
Posts: 20
Joined: Wed May 14, 2014 11:40 am

Re: PowerShell and HTML Table

Post by waynecierkowski » Fri Jun 08, 2018 6:59 am

Thanks... I'll give this method a try...

User avatar
waynecierkowski
Posts: 20
Joined: Wed May 14, 2014 11:40 am

Re: PowerShell and HTML Table

Post by waynecierkowski » Fri Jun 08, 2018 7:31 am

That works great but now my detail lines are in Bold text versus non-bold text.
SampleOutput.png
SampleOutput.png (15.53 KiB) Viewed 192 times
Is there a way to have non-bold text in my detail lines.

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell and HTML Table

Post by jvierra » Fri Jun 08, 2018 8:06 am

To gain good control over HTML it would be best that you create aa collection of objects then use "ConvertTo-Html" to create the report. To format the table an all of the report you would use a CSS style sheet or header style block.

Even with a hand built html you can add a style sheet. Just target the style sheet at the tags that you want to style.

The links I posted have some examples of using a style sheet.

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: PowerShell and HTML Table

Post by jvierra » Fri Jun 08, 2018 11:26 am

Sorry. I had a copy/paste error with the roqq template. Here is the fixed version.

Code: Select all

$tblRowTemplate = @'
<tr>
    <td>{0}</td>
    <td>{1}</td>
    <td>{2}</td>
    <td>{3}</td>
    <td align='right'>{4:N0}</td>
    <td align='right'>{5:N0}</td>
    <td>{6}</td>
</tr>
'@

Post Reply