This topic contains 5 replies, has 3 voices, and was last updated by ofergenat 2 years, 3 months ago.
-
AuthorPosts
-
January 4, 2016 at 3:14 pm #33451
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
-
January 4, 2016 at 3:43 pm #33452
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.
-
January 4, 2016 at 10:17 pm #33464
Thank you.
Can I have a clue why is my code not a valid HTML page? -
January 4, 2016 at 10:21 pm #33465
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.
-
January 5, 2016 at 3:33 am #33472
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 -
January 5, 2016 at 9:33 am #33502
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
-
AuthorPosts
You must be logged in to reply to this topic.