Custom HTML table

Hello,
i am using invoke-webrequest to collect some data of students from 2 different internal web pages and want that to merge in a single HTML table, so far i have no success, i am collecting the data from allelements property of invoke-webrequest from different classes of the HTML pages.

$marks9=($class9.AllElements | ? { $_.Class -eq 'marksentered' } | select innerText).innertext
$attendance9=($class9.AllElements | ? { $_.Class -eq 'attendance' } | select innerText).innertext
$leavedays9=($class9.AllElements | ? { $_.Class -eq 'leavedays' } | select innerText).innertext


$marks10=($class10.AllElements | ? { $_.Class -eq 'marksobtainedinexam' } | select innerText).innertext
$attendance10=($class10.AllElements | ? { $_.Class -eq 'attended' } | select innerText).innertext
$leavedays10=($class10.AllElements | ? { $_.Class -eq 'absentdays' } | select innerText).innertext

From these data i want to have a table of values of the variables as follows

in html

So… without knowing what that data all looks like, it’s a little difficult to guess at the answer. I guess, assuming your variables all contain the data you want, you’d have to manually construct the HTML table using TABLE, TR, and TD tags. It’s difficult to use the HTML angle bracket symbols here, so I’ll use [ and ] instead.

$html = @"
 [table]
  [tr]
    [td]$marks9[/td]
    [td]$attendance9  $leavedays9[/td]
  [/tr]
  [tr]
    [td]$marks10[/td]
    [td]$attendance10  $leavedays10[/td]
  [/tr]
 [/table]
"@

Again, assuming your variables contain the correct data, this would produce an HTML table similar to what you have in the image you provided.

THank you Don,it works… how can i construct a nice looking table with this HTML.

Is it also possible to create a custom table with that data and convert to html? beacuse i need some sorting there…

For “nice looking,” you’ll mainly need to learn HTML and CSS. PowerShell can’t do that for you. Its native HTML capabilities are very basic.

“Custom table with that data and convert to HTML” I’m not sure I understand. You might read, “Creating HTML Reports in PowerShell,” which is on our eBooks menu. Perhaps that will help answer the questions. Again, though, “nice looking” will require you to learn some HTML and CSS.

Is it also possible to create a custom table with that data and convert to html? beacuse i need some sorting there…

Hello Don,
The HTML table which i created now is not dynamic, i want to sort the data with marks and i dont think i can do that in HTML…so if i can create a powershell custom table with the variables i have i can sort them…can you please help me on how can i create a custom table with the list of variables i have… if the custom table is from a single cmdlet it seems to be say, but since the data here is from different places i am struggling a bit…

No, you can’t sort HTML easily. You’re right.

But I don’t know what “a custom PowerShell table” is. As far as I know, there is no such thing. PowerShell’s only use of tables is as a display option (Format-Table), but any data manipulation - sorting and so on - happens before that.

My point with asking you to read the book is that is shows how to put your data into custom objects. PowerShell can sort objects. Once sorted, you can convert the objects into a table for screen display (Format-Table) or convert it to HTML. Custom objects let you combine data from multiple places, and each object becomes a “row” in your final output.

It might be worth reading “Learn PowerShell Toolmaking in a Month of Lunches,” as that covers some of the basic concepts where I think you’re struggling. But, the HTML Reports ebook also covers the custom objects - you’ll notice that its examples use several custom objects. Whether you want dynamic tables or not isn’t the point.

Although, the dynamic tables do support click-to-sort columns.

Hi
I suggest you to read Don ebook https://devopscollective.gitbooks.io/creating-html-reports-in-powershell/content/
or you can use pscookiemonster script https://gallery.technet.microsoft.com/scriptcenter/PowerShell-HTML-Notificatio-e1c5759d to create dynamic html page , but as don mentioned if you have to do sorting and format data all needs to be done before you are passing it to html . I used both of their methods to and they are really useful. Hope it helps :slight_smile:

can we have multiple values for each key in pscustomobject? or i should create multiple pscustomobject with same key names? if that is the case can this be dynamic?

Objects don’t have keys; they have property names.

And you can only attach one object to a property. However, that object could be a delimited string, or it could be a collection of other objects. However, ConvertTo-HTML can’t expand collections like that, so you would want to stick with one object (“value,” if you prefer) per object.

I think part of what you may be struggling with is that you’re not thinking along the lines of a proper PowerShell tool. So you’re fighting what PowerShell wants to do, and now it wants to work, and so it isn’t helping you as much as it could. My book, “Learn PowerShell Toolmaking in a Month of Lunches,” covers the “right” way to build tools.

But as a very brief example:

Function Get-StudentInfo {
 # assume $students is a list of student IDs or whatever
 foreach ($student in $students) {
  $grades = Get-StudentGrades -StudentID $student.id
  foreach ($grade in $grades) {
    $props = @{ 'StudentName' = $student.Name
                'Grade' = $grade.gradeletter }
    New-Object -Type PSObject -Prop $props
  }
 }
}

So I’m getting all the information, and combining it in a PSObject, and then outputting one at a time to the pipeline. Each object represents what I want as a “row” in the final HTML.

Get-StudentInfo | ConvertTo-HTML

Will result in a decent-looking table. If you used my EnhancedHTML2 module, you could have more control over the table appearance, if you wanted.

But PowerShell does not have unlimited abilities to turn hierarchical data into nice-looking HTML. If that’s what you want, you’re going to have to hand-code the HTML on your own, and it’s going to require you to understand both HTML and CSS. Those aren’t PowerShell topics, though.