Create an HTML table from variables

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of ofergenat ofergenat 11 months ago.

  • Author
    Posts
  • #33451
    Profile photo of ofergenat
    ofergenat
    Participant

    I am having hard time to understand how to get this to work.
    I already have a script that creates a report using a custom CSS and using "ConvertTo-HTML" from some cmdlets I use, this works fine.
    Now I have a new script I wrote, an I am trying to create the same HTML table but this time I only want to use some variables from my script to add them into a table using two columns.
    I tried to put the vars into a hash table and convert it to HTML – not working.
    I tried to create a PSObject and convert it to HTML – not working.
    I would appreciate if someone here would please give some advise how to change this code and create a 2 column table that contains the name,value of the hash table or otherwise use only the variables created (maybe by using a PSObject?)

    Snippet from the script:

    $Css='table{margin:auto; width:98%}
                  Body{background-color:LightSteelBlue; Text-align:Center;}
                    th{border-width: 1px;padding: 0px;border-style: solid;border-color: black; background-color:black; color:white;}
                    td{border-width: 1px;padding: 0px;border-style: solid;border-color: black; background-color: #D3D3D3; color:Black; Text-align:Center;}
         '
    
    $Report  = ''
    $Report += ''
    $Report += "`n`t"
    $Report += $Css
    
    # Fiddle with browser mode
    $Report += "`n" + '' #''      
    $Report += "`n`t`n`n"
    $Report += "PowerShell Audit ReportThis report was generated @: $(Get-Date)=========================================="
    ## Variables
    $FName = 'Bila'
    $LName = 'Bong'
    $Age = '120'
    ## Create a table
    $Hash = @{"FName" = $FName ; "LName" = $LName ; "Age" = $Age}
    $Table = $Hash | ConvertTo-Html -Fragment -As Table | Out-String
    
    $Report += "$Table "
    $Report += "===== DONE >====="
    $Report +=""
    $Report += "`n`n"
    # R debug
    #$Report += "document.body.innerHTML += navigator.userAgent.toLowerCase();"
    $Report | Out-File $PWD\test.htm ; Invoke-Expression $PWD\test.htm
    
  • #33452
    Profile photo of Don Jones
    Don Jones
    Keymaster

    ConvertTo-HTML won't work well with a hash table, but with a custom object it should be fine.

    Understand that what you're producing isn't going to be a valid HTML document, but I don't know if that's your goal. You might read, "Creating HTML Reports in PowerShell" (free ebook here) for examples of how to do this in proper HTML, if that's of interest.

    But anyway, to your question:

    $objects = @()
    $objects += New-Object -Type PSObject -Prop @{'FName'='Lisa';'LName'='Simpson'}
    $objects += New-Object -Type PSObject -Prop @{'FName'='Bart';'LName'='Simpson'}
    $objects += New-Object -Type PSObject -Prop @{'FName'='Home';'LName'='Simpson'}
    $objects | ConvertTo-HTML -As Table -Fragment | Out-String
    

    Table will contain two columns and three rows. A hash table will always produce a "Keys" and "Values" column, which isn't what I think you want.

  • #33464
    Profile photo of ofergenat
    ofergenat
    Participant

    Thank you.
    Can I have a clue why is my code not a valid HTML page?

  • #33465
    Profile photo of ofergenat
    ofergenat
    Participant

    Also, I didn't try yet your example but from first reading I wonder how do I choose the columns names as tgere is no 'select' here.

  • #33472
    Profile photo of Chris Bakker
    Chris Bakker
    Participant

    Breakdown the problem, you got a few i think.
    In $table you get a System.Collections.Hashtable+ValueCollection.
    To solve this use:
    $Table = $Hash.GetEnumerator() | ConvertTo-Html -Fragment -As Table

  • #33502
    Profile photo of ofergenat
    ofergenat
    Participant

    Thanks Chris that one worked, I just added "select" to avoid receiving both name,key,value

    $Table = $Hash.GetEnumerator() | select name,value | ConvertTo-Html -Fragment -As Table

You must be logged in to reply to this topic.