How do I display output in Format-List style?

#1

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!

0 Likes

#2

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"
}
0 Likes

#3

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.

1 Like

#4

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

2 Likes

#5

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
}
0 Likes

#6

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

#7

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

0 Likes