2

(1) I expected the following statement

@(1,2,3) | ConvertTo-Html -Fragment

returns

<table>
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
</table>

but it returns

<table>
</table>

?


(2) And

'11','22','33' | ConvertTo-Html -Fragment

returns

<table>
<colgroup><col/></colgroup>
<tr><th>*</th></tr>
<tr><td>2</td></tr>
<tr><td>2</td></tr>
<tr><td>2</td></tr>
</table>

?

Expected:

<table>
<colgroup><col/></colgroup>
<tr><th>My Column Name</th></tr>
<tr><td>11</td></tr>
<tr><td>22</td></tr>
<tr><td>33</td></tr>
</table>

1 Answer 1

5

It expects you to be providing an array of objects. So for example two, your expected output even says 'My Column Name' there, but you never provided that to anything. Also, you have to specify what properties you want the table to contain or it defaults to '*' to imply everything. So what you can do is something like:

'11','22','33' | ForEach{[PSCustomObject]@{'My Column Name'=$_}} | ConvertTo-HTML -Fragment -Property 'My Column Name'

Your output would then be:

<table>
<colgroup><col/></colgroup>
<tr><th>My Column Name</th></tr>
<tr><td>11</td></tr>
<tr><td>22</td></tr>
<tr><td>33</td></tr>
</table>

You can achieve the same effect by using the Select command as such:

'11','22','33' | Select @{label='My Column Name';expression={$_}} | ConvertTo-HTML -Fragment -Property 'My Column Name'
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.