1

I want to use PS to create an HTML report with multiple tables, similar to what is summarized here quite succinctly. I have tried many different ways, however I can't seem to get the values to populate the HTML table fragments correctly.

What am I missing or doing incorrectly?

To use one small table as an example, let's say I want to list the installed versions of .NET on a system.

I can populate an array of objects using:

$netver = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
Get-ItemProperty -name Version,Release -EA 0 |
Where { $_.PSChildName -match '^(?!S)\p{L}'} |
Select PSChildName, Version, Release |ft;

... and the values are there correctly (checking with just $netver).

According to the page referenced, as well as other pages referencing the same, I should just need to do something like the following:

$netverhtml = $netver | ConvertTo-Html -AS Table -Fragment -Property * | out-string;

...and the fragment should be ready to include when building the final html page, with something similar to this...

ConvertTo-HTML -body "$netverhtml" |   Out-File $logfile

... however even before I get there, the fragment ($netverhtml) doesn't contain the data (similar to what is in $netver), let alone when the html page is finally constructed.

Unless I use a string, I keep ending up with formatting data in the HTML table structure (end result below).

<table>
<colgroup><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>ClassId2e4f51ef21dd47e99d3c952918aff9cd</th><th>pageHeaderEntry</th><th>pageFooterEntry</th><th>autosizeInfo</t
h><th>shapeInfo</th><th>groupingEntry</th></tr>
<tr><td>033ecb2bc07a4d43b5ef94ed5a35d280</td><td></td><td></td><td></td><td>Microsoft.PowerShell.Commands.Internal.Form
at.TableHeaderInfo</td><td></td></tr>
<tr><td>9e210fe47d09416682b841769c78b8a3</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>4ec4f0187cb04f4cb6973460dfe252df</td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td>cf522b78d86c486691226b40aa69e95c</td><td></td><td></td><td></td><td></td><td></td></tr>
</table>

Even though $netver is an array of objects, I have also tried creating new PSObjects, trying different methods to populate object properties with correct values...

$tableobjs = @();
foreach ($n in $netver){
$tmpname = ($n).pschildname;
$tmpver = $n.version|out-string;
$tmprel = $n.release;
$tableobjs += New-Object -Type PSObject -Prop @{
'PSChildName'='$tmpname';
'Version'='$tmpver';
'Release'='$tmprel';
}}` 

but the values don't populate each object no matter what I have tried. It appears to iterates through each object once (because the number of lines is the same), but the values (from $netver)just don't populate the objects.

PS C:\> $tableobjs
Version            Release               PSChildName
-------            -------               -----------
$tmpver            $tmprel               $tmpname
$tmpver            $tmprel               $tmpname
$tmpver            $tmprel               $tmpname
$tmpver            $tmprel               $tmpname
$tmpver            $tmprel               $tmpname
$tmpver            $tmprel               $tmpname
$tmpver            $tmprel               $tmpname

When I display them in the HTML body the values of course just display what is inside the $fragment.

Again... the question is what am I doing wrong that is preventing the fragment from populating with the actual values, instead of table formatting info? I am more than willing to try suggestions, convert to other object types, or try another approach to make this happen... but obviously displaying a string in an HTML page (the only way I have seen the data in the HTML page first converting using $netver = $netver | out-string first) isn't a readable option.

I don't think it matters for this example, but I am using PS 3.0 at the moment.

3
  • 1
    Your problem is the final |ft this stringifies the output - there are no more objects which are needed for ConvertTo-Html. Also the |out-string shouldn't be neccessary. Commented Dec 6, 2018 at 23:36
  • 2
    Good point about ft, @LotPings, but the output objects aren't strings, they're_formatting objects_, and it is their properties that are being used by ConvertTo-Html. Commented Dec 6, 2018 at 23:42
  • 1
    Thanks all! This was really bugging me. So basically once format table is done it is a string object, and you can’t access the properties any longer because the resulting “table” string doesn’t have them to access. I should have taken a step back and figured it out myself, but I was stuck and appreciate the time saving point in the right direction. Commented Dec 7, 2018 at 1:04

1 Answer 1

2

With the |Format-Table removed , you should get better results,
of course mklements explanation is better than mine.

$netver = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -recurse |
  Get-ItemProperty -name Version,Release -EA 0 |
    Where { $_.PSChildName -match '^(?!S)\p{L}'} |
      Select PSChildName, Version, Release

$netverhtml = $netver| ConvertTo-Html -AS Table -Fragment -Property *

ConvertTo-HTML -body "$netverhtml" | Out-File $logfile
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.