PS Table-view is a very useful view to present collection of PSObjects. This works well with PSObejects with just simple and single value properties, such as name, age, telephone number etc. However, if there is any properties with collections, the layout may be too for for those long list of collection objects.
I find out a great way to convert collection of PSObjects with collections as its properties. It works very well to break collection of values as multiple lines in column so that the layout of table view is much tight and clean.
Here is the function:
#FileName: UtilCollection.ps1
#================================
#FUNCTION LISTINGS
#================================
Function ConvertPSObjectCollection {
<#
.SYNOPSIS
Gets a converted collection of PSObjects so that the converted
collection can be nicely displayed in table-view.
.DESCRIPTION
This function will convert a collection of PSObjects with
one or more array properties to a collection of PSObjects so
that the colleciton can be output to a nice and tight table view.
The input collection contains PSObjects with one or more array
property element. Normally array elements are too wide to be displayed
in a table-view. By converting collection, the returned colection
can be viewed nicely in table-view.
This function is based on SO solution http://stackoverflow.com/questions/22723954/powershell-autosize-and-specific-column-width
.PARAMETER CollectionPSObjects
Mandatory. Collection of PSObjects, PSObject have one or more
property of array data.
.INPUTS
Parameters above
.OUTPUTS
A collection of converted PSObjects with same prorperties
but array elements as rows in the collection so that the
collection can be nicely formated output to a table view.
.NOTES
Version: 1.0
Authors: David Chu
Creation Date: 10/04/2014
Purpose/Change: Initial function development
.EXAMPLE
$myCol = @(
(New-Object –TypeName PSObject –Prop @{'id'='01';'name'='a';'items'=@(1,2,3);'others'=@('SampleA1','SampleA2')}),
(New-Object –TypeName PSObject –Prop @{'id'=@('02a','02b');'name'='b';'items'=@(1,2,3);'others'=@('SampleB1','SampleB2','SampleB3','SampleB4','SampleB5')}),
(New-Object –TypeName PSObject –Prop @{'id'='03';'name'=@('c1','c2');'items'=@(1,2,3);'others'='SampleC'})
)
$myCol1 = ConvertPSObjectCollection $myCol
$myCol1 | FT ID,Name,Items,Others -AutoSize
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)] $CollectionPSObjects
)
Process {
$m_result = $CollectionPSObjects | %{
$Current = $_
$Members = $_|GM|?{$_.MemberType -match "Property"}| `
Select -ExpandProperty Name
$Rows = ($Members|%{$current.$_.count}| `
sort -Descending|Select -First 1)-1
For($i=0; $i -le $Rows;$i++){
$LoopObject = New-Object PSObject -Property `
@{$($Members[0]) = if($Current.$($Members[0]).count -gt 1) { `
$Current.$($Members[0])[$i] `
} else{ `
if(!($i -gt 0)){ `
$Current.$($Members[0]) `
}else{ `
$Null `
} `
} `
}
If($Members.Count -gt 1){
$Members[1..$Members.count]|%{
Add-Member -InputObject $LoopObject `
-MemberType NoteProperty `
-Name $_ `
-Value $(if($Current.$_.count -gt 1) { `
$Current.$_[$i] `
}else { `
if(!($i -gt 0)) { `
$Current.$_ `
}else{ `
$Null `
} `
} `
)
}
}
$LoopObject
}
}
return $m_result
}
}
References
- SO Question: Autosize and Specific Column Width
0 comments:
Post a Comment