How do I display output in Format-List style?

I have an input form and after it inserts the data into the database table, I want the post-input page to display the input data in a list format similar to Format-List. What elements can be used to accomplish this? Can someone provide an example (real or mocked up)? Thanks!

Not a great layout but this is my current solution.

New-UDCard -Content {
    New-UDParagraph -Text $Text -Color $Color
    New-UDParagraph -Text "System = $System"
    New-UDParagraph -Text "Description = $Description"
    New-UDParagraph -Text "URL = $URL"
    New-UDParagraph -Text "Stakeholder = $StakeholderDept"
    New-UDParagraph -Text "Contact = $Contact"
    New-UDParagraph -Text "EscalationContact = $EscalationContact"
}

You might opt to do pure HTML output into of UD objects:

  $Processes = Get-Process -Name chrome 


    ForEach($Process in $Processes)
    {
        $Process | Select-Object -Property ProcessName, Id, WS 
        
        New-UDHtml -Markup ("<b>ID</b>: " + $Process.id + "<br>")
        New-UDHtml -Markup ("<b>Name</b>: " +$Process.name + "<br>")
        New-UDHtml -Markup ("<b>WS</b>: " +$Process.WS + "<br>")
        New-UDHtml -Markup ("<br>")
     
    }

Gets:
image

NOTE : there are probably a number of cool ways you could format this object into HTML using a separate formatter function, so you can avoid hardcoding things like in my example.

3 Likes

This would make it prettier. (Well, it’s an uglier font, but everything lines up right.)

( ( Get-Process -Name chrome |
    Select-Object -Property ProcessName, Id, WS |
    Format-List |
    Out-String
    )  -replace ' ', '&nbsp;'
    ) -split [environment]::NewLine |
    ForEach {
        If ( $_ ) { New-UDHtml -Markup "<span style=""font-family:monospace"">$_</span>"  }
        Else      { New-UDHtml -Markup '<br>' } }

Thanks,
Tim Curwick

3 Likes

Sharing in case others are interested… I ended up using the following to transpose the data and then display it with New-UDTable. (not a fan of the function name but I such at naming).

Function ConvertTo-FormatListView {
    param(
        $Data
    )
    $transposeData = @()
    $Column = $Data.psobject.properties | Select-Object -ExpandProperty Name
    for ($i = 0; $i -lt ($Column | Measure-Object).count; $i++) {
        $transposeData += [PSCustomObject]@{
            Name  = $Column[$i]
            Value = $Data.$($Column[$i])
        }
    }
    $transposeData
}

Al,

You can simplify your syntax (and with a minor improvement in performance) with something like this.

Function ConvertTo-FormatListView
    {
    Param (
        $Data )

    ForEach ( $Property in $Data.psobject.properties.Name )
        {
        [PSCustomObject]@{
            Name  = $Property
            Value = $Data.$Property }
        }
    }

But that will only work with a single object. If you want to be able to pipe multiple objects to it, do something like this:

Function ConvertTo-FormatListView
    {
    [cmdletbinding()]

    Param (
        [parameter( ValueFromPipeline = $True )]
        [object[]]
        $Data )

    Process
        {
        ForEach ( $Object in $Data )
            {
            ForEach ( $Property in $Object.psobject.properties.Name )
                {
                [PSCustomObject]@{
                    Name  = $Property
                    Value = $Object.$Property }
                }
            }
        }
    }

Thanks,
Tim Curwick

1 Like

Actually, we can do better.

For your single object example:

Function ConvertTo-FormatListView
    {
    Param (
        $Data )

    $Data.psobject.Properties | Select-Object -Property Name, Value
    }

For multiple objects:

Function ConvertTo-FormatListView
    {
    Param (
        $Data )

    $Data.psobject.Properties | Select-Object -Property Name, Value
    }

Function ConvertTo-FormatListView
    {
    [cmdletbinding()]

    Param (
        [parameter( ValueFromPipeline = $True )]
        [object[]]
        $Data )

    Process
        {
        ForEach ( $Object in $Data )
            {
            $Object.psobject.Properties | Select-Object -Property Name, Value
            }
        }
    }

(Slightly slower than the previous example, but not enough to measure, but less care about.)

Thanks,
Tim Curwick

Thanks for posting this man, I needed some help formatting my text file I wanted on separate lines and this worked beautifully. Much appreciated sharing real-world examples

Wait, couldn’t this just be accomplished by throwing the returned data at an UDGrid?

Well not quite that simple for me…I was grabbing the $user and get-date and outputting this to a visitor txt file, then appending to that file each time…this then meant I had a text file that said something like “adamb logged on 04/10/2019 11:26” this is because a manager wanted to know who was actually was using the dashboard. So then I put a real small sneaky visitors link, which then outputs that text file line by line with a space inbetween lines into a modal window. So now I got a little log file of people visiting the dashboard and if you know where the sneaky link is you can view it…which now means I don’t have to email that manager results as he can view it himself if he visits the dashboard…the problem I was having get-content of the text file was returning one super long line, not line by line. But using the technique @leeberg shared I was able to then use that method to display a text file log nicely in a modal window.