1

I use ConvertTo-HTML to convert a list of objects into a table. The only problem is that I cannot define the order of the columns in that table. I want a specific property (hostname) for all of the objects to be the first column in the table. Is there any way to do this?

Example Code:

function Create-MyObject {
    param(
        $name
    )

    $object = New-Object -TypeName PSObject -Property @{
        "Name" = $name
        "Prop1" = Get-Property1 $name
        "Prop2" = Get-Property2 $name
        "Prop3" = Get-Property3 $name
    }

    return $object
}

$myarray = @()

foreach($value in $list)
{
    $myarray += Create-MyObject -name $value
}

Add-Content -Value $(ConvertTo-HTML $myarray | Out-String) -Path "C:\Temp\output.html"
2
  • 2
    Can we see the code? Objects created from regular hash table don't maintain the property order. Typically you fix that with an ordered hash table, or doing a Select-Object to re-build them in the right order after they're created. Commented Apr 5, 2014 at 1:32
  • @mjolinor Although it's not the exact code I'm using, this is the gist of it. When I open the HTML, it might show the columns in the order Prop3,Prop1,Name,Prop2 (it might also be alphabetical, but I don't have the actual code to look at at the moment). I want Name to be first in the list of columns. Commented Apr 5, 2014 at 21:29

1 Answer 1

1

Here is one solution that modifies the function to create the object from an ordered hash table so that the order is maintained. This requires V3 or better.

function Create-MyObject {
    param(
        $name
    )

    $object = New-Object -TypeName PSObject -Property [ordered]@{
        "Name" = $name
        "Prop1" = Get-Property1 $name
        "Prop2" = Get-Property2 $name
        "Prop3" = Get-Property3 $name
    }

    return $object
}

 $myarray = @()

    foreach($value in $list)
    {
        $myarray += Create-MyObject -name $value
    }

Add-Content -Value $(ConvertTo-HTML $myarray | Out-String) -Path "C:\Temp\output.html"

Heres's another solution using Select-Object in the main script to reorder the properties before it's converted. This will work on V2 or better:

$myarray = @()

foreach($value in $list)
{
    $myarray += Create-MyObject -name $value
}

Add-Content -Value $(ConvertTo-HTML $myarray | Out-String) -Path "C:\Temp\output.html"



function Create-MyObject {
    param(
        $name
    )

    $object = New-Object -TypeName PSObject -Property @{
        "Name" = $name
        "Prop1" = Get-Property1 $name
        "Prop2" = Get-Property2 $name
        "Prop3" = Get-Property3 $name
    }

    return $object
}

$myarray = @()

foreach($value in $list)
{
    $myarray += Create-MyObject -name $value
}

$myarray = $myarray | select Name,Prop1,Prop2,Prop3

Add-Content -Value $(ConvertTo-HTML $myarray | Out-String) -Path "C:\Temp\output.html"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.