Home > database >  Having an issue with writing correct information to htmlfile with powershell convertTo-html
Having an issue with writing correct information to htmlfile with powershell convertTo-html

Time:10-21

This question has 2 parts: Ultimately I am trying to print out the data from the filedescription and originalfilename. The first and probably simple question is how do I get those on the same line? I am using PS G:\SysinternalsSuite> $values=@("filedescription", "originalfilename");foreach($V in $Values){(get-command g:\*\*\a*.exe).fileversioninfo.($v)} Windows Assessment and Deployment Kit - Windows 10 adksetup.exe Two lines, not one. I can edit it afterward, but...

The next issue is trying to output this information to a file: I am using (get-command g:\*\*\a*.exe).fileversioninfo.filedescription to return the prettified name of a folder of exe files (in this eg. I was working with SysInternalsSuite) Result:

PS G:\SysinternalsSuite> (get-command g:\*\*\a*.exe).fileversioninfo.filedescription
Windows Assessment and Deployment Kit - Windows 10

Worked beautifully... Then it went all wrong! My next idea was to put these values into an HTML file, so I did this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/></colgroup>
<tr><th>*</th></tr>
<tr><td>50</td></tr>
</table>
</body></html>```

WTH are all these numbers??? Where's my data? Fine...
```PS G:\SysinternalsSuite> (get-command g:\*\*\a*.exe).fileversioninfo.filedescription | echo
Windows Assessment and Deployment Kit - Windows 10``` 
Perfect!
```PS G:\SysinternalsSuite> (get-command g:\*\*\a*.exe).fileversioninfo.filedescription | echo | convertTo-HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/></colgroup>
<tr><th>*</th></tr>
<tr><td>50</td></tr>
</table>
</body></html>
write GRRRR!!!!

Ok! What am I missing here?

CodePudding user response:

ConvertTo-Html works by inspecting the properties of whatever object you pipe to it, and creates a table where each column corresponds to the property name.

Since the [string] type only has one property - the Length property - that's what you get in the html table - a single column with the length of each input string.

Instead, send an object where the description is the value of a property, then specify the list of properties you want in the table:

(Get-Command g:\*\*\a*.exe).FileVersionInfo |ConvertTo-Html FileDescription

CodePudding user response:

function HtmlTable
{
     [OutputType([System.Object])]

    Param (
        
        [Parameter(Mandatory=$True)]
        [String[]]
        $tableRows,
        [Parameter(Mandatory=$True)]
        [String[]] 
        $tableHeaders,
        [Parameter(Mandatory=$True)] 
        $tableWidthPercentage
    )        
    $htmTable = "<html>
                <style>
                body {font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;}
                table{width: $tableWidthPercentage;border-collapse: collapse;}
                th{background-color:#106ebe; color:white;font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;border: 1px solid black;padding: 4px;text-align: left;border: 1px solid #ddd;}
                tr:nth-child(even){background-color: #f2f2f2;}
                td{font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;border: 1px solid #ddd;padding: 4px;}
                </style>
                <table>
                <tr>"
    Foreach ($header in $tableHeaders)
    {
        $htmTable  =  "<th>$header</th>"
    }
   
    $htmTable  ="</tr>
                $tableRows
                </table>"
    return $htmTable
}
#simply you can call the function directly as below, where $tableData is the variable you can give the td data
$htmlTemplate= HtmlTable -tableWidthPercentage 80% -tableRows $tableData  -tableHeaders ID,CreatedDate,Title,State,Efforts
  • Related