red888
red888

Reputation: 31620

Force Convertto-html to only include an object's "MemberType" "property"

I have an array of system.data.datarow objects. Now the properties of these objects have the info I want. When I pass these objects to convertto-html though it picks up all this extra crap and I see rows with names like RowError,RowState,Table- when all I want is the objects properties.

Is there a way I can only include the object's properties to be converted to html (meaning if I do a Get-Member on the object the "MemberType" property).

Can I convert these objects to generic psobjects without having to loop through them all and rebuild them with New-Object?

Upvotes: 0

Views: 663

Answers (2)

Brett
Brett

Reputation: 931

The only problem with the accepted answer is that it changes the order of the fields to alphabetical from the given field names

To avoid and keep the original order from the select statement, I used the following approach

$unwantedColumns = @('RowError','RowState','Table','ItemArray','HasErrors') # these guys get added automatically by Invoke-SqlCmd

$props = $sqlResult.PSObject.Properties.Name | Where-Object {$_ -NotIn $unwantedColumns}

$htmlBody = $sqlResult |
        Select-Object -Property $props |
        ConvertTo-Html @ConvertToHtmlArgs |
        Out-String

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174760

You can grab the names of all the Property-type properties with Get-Member and Select-Object:

$Props = Get-Member -InputObject $DataRowObjects[0] -MemberType Property | Select-Object -ExpandProperty Name
$Html = $DataRowObjects | Select-Object -Property $Props | ConvertTo-Html

Upvotes: 1

Related Questions