H Bala H Bala - 3 years ago 273
Powershell Question

converto-html breaks the table, tried with PSobject as input

I get a strange output with Converto-HTML on Azure function for the following:
I am trying to build a HTML and pipe it to Converto-HTML and send email with style. The output is table with header column with value '*' and data row column value as 146 or 189 or sort.

Script :

$applicationtable1 = "< table>< th>Application Name< /th>< th>Requested By< /th>< th>Requested Date< /th>< /tr>"
$applicationtable1 += "< tr>< td> hi < /td>< td> hi < /td> < td> hi < /td>< /tr> < /table>"
$Body = New-Object PSObject
$Body | Get-Member #validating proper ps object
$Body = $applicationtable1

$htmlBody = $Body | ConvertTo-HTML | out-string

write-host $htmlBody




Local Output

PS C:\Users\292883> $Body = New-Object PSObject

PS C:\Users\292883> $Body = $applicationtable1

PS C:\Users\292883> $htmlBody = $Body | ConvertTo-HTML | out-string

PS C:\Users\292883> write-host $htmlBody

I tried to create a PS object and then pipe it as well but it appears both gets me same output.

Also, tried instead of PSObject, using PSCustomObject as below. it appears working but still output got a merged header table row.

$results = @()
$applicationtable1 = "< table>< tr>Application Name< /tr>< tr>Requested By< /tr>< tr>Requested Date< /tr>< /tr>"
$applicationtable1 += "< tr>< td> hi < /td>< td> hi < /td> < td> hi < /td>< /tr> < /table>"

$item = New-Object -Type PSCustomObject -Property @{
'html' = $applicationtable1

$results = $item | ConvertTo-HTML | out-string

**Output Fragment which still appears even if I dont have header row **

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

Answer Source

ConvertTo-Html takes an object and provides you with an HTML representation of that. What you're creating is going to generate an HTML table with a single column with the title "Html". All content of that table is subject to HTML encoding (< will become < and so on).

If you wish to use ConvertTo-Html you must feed it an Object.

$item = [PSCustomObject]@{
    'Application Name' = 'hi'
    'Requested By'     = 'hi'
    'Requested Date'   = 'hi'
$item | ConvertTo-HTML | out-string

You may include PreContent and PostContent in raw HTML using their respective parameters. Similarly you may include CSS styles and so on using the Head parameter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download