Custom HTML table

This topic contains 10 replies, has 3 voices, and was last updated by Profile photo of Don Jones Don Jones 2 months, 1 week ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #47125
    Profile photo of Powershell
    Powershell
    Participant

    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
    http://i.imgur.com/4fKgUCH.png

    in html

    • This topic was modified 2 months, 2 weeks ago by Profile photo of Powershell Powershell.
    • This topic was modified 2 months, 2 weeks ago by Profile photo of Powershell Powershell.
    • This topic was modified 2 months, 2 weeks ago by Profile photo of Powershell Powershell. Reason: Added image
    #47132
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    • This reply was modified 2 months, 2 weeks ago by Profile photo of Don Jones Don Jones.
    • This reply was modified 2 months, 2 weeks ago by Profile photo of Don Jones Don Jones.
    #47157
    Profile photo of Powershell
    Powershell
    Participant

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

    #47169
    Profile photo of Powershell
    Powershell
    Participant

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

    #47172
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    #47190
    Profile photo of Powershell
    Powershell
    Participant

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

    #47203
    Profile photo of Powershell
    Powershell
    Participant

    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..

    • This reply was modified 2 months, 2 weeks ago by Profile photo of Powershell Powershell.
    #47216
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    #47320
    Profile photo of Suresh krishnan
    Suresh krishnan
    Participant

    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 🙂

    #47499
    Profile photo of Powershell
    Powershell
    Participant

    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?

    #47501
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    • This reply was modified 2 months, 1 week ago by Profile photo of Don Jones Don Jones.
Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.