Powershell: Autosize and Specific Column Width

假如想象 提交于 2019-11-29 16:02:14

A slightly different approach here. Loop through your collection, for each set find the count for each property and select the highest count. Run that set through that many loops, and on each loop create a custom object where each property checks to see if it is an array. If it is it iterates into that array, if it is not it checks if this is the first round of custom objects and returns the value, else it returns a blank. The output is what you are looking for, and -AutoSize for FT works perfectly.

First I made a collection similar to yours:

$col = @(

    (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'})
    )

Then I ran that through my suggested code:

$Col|%{
    $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
    }
}|FT ID,Name,Items,Others -AutoSize

It gave me this output:

id  name items others  
--  ---- ----- ------  
01  a        1 SampleA1
             2 SampleA2
             3         
02a b        1 SampleB1
02b          2 SampleB2
             3 SampleB3
               SampleB4
               SampleB5
03  c1       1 SampleC 
    c2       2         
             3 

Edit: Ok, updated my code. It no longer cares what array you throw at it, how many properties it has, or how many properties those properties have. Just so long as the collection given it contains only strings and/or arrays it will output the desired collection of objects you want, like my output noted above.

I see what you mean, and I have no answer staying within the console, but if you do this to send it to Out-GridView:

$col = @(

    (New-Object –TypeName PSObject –Prop @{'id'='01';'name'='a';'items'=@('the first item','the second item', 'the third item')}),
    (New-Object –TypeName PSObject –Prop @{'id'='02';'name'='b';'items'=@('the first item','the second item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item', 'the third item')}),
    (New-Object –TypeName PSObject –Prop @{'id'='03';'name'='c';'items'=@('the first item','the second item', 'the third item')})
    )

$col|Select -p id,@{E={$_.items -join "`n"};L="Items"},name | Out-GridView

(I trimmed the 'Expression' and 'Label=' so they fit on the screen).

Then the GridView shows the columns with the right width.

So if GridView can, why can't Format-Table? Maybe you can get around it with a custom view - http://www.adminarsenal.com/admin-arsenal-blog/bid/43912/PowerShell-Tips-for-System-Administrators-Format-Views, I haven't tried that approach.

Here is an example

F:\> $array|format-table RecordType,
$array|Format-Table @{Label= "RecordType";Expression={ $_.RecordType};             Width = 10 }, `
                @{Label= "Date"      ;Expression={ $_.date};                   Width = 8 },`
                @{Label= "Time"      ;Expression={ $_.Time};                   Width = 8 },`
                @{Label= "code"      ;Expression={ $_.code}},`
                @{Label = "sales"    ;Expression={ [math]::Round($_.sales,2)} }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!