Powershell: Autosize and Specific Column Width

前端 未结 3 1540
没有蜡笔的小新
没有蜡笔的小新 2020-12-20 02:18

Based on this SO question Powershell: Output collection of objects, I am able to wrap a collection of strings in multiple lines in one column. However, when I try to output

相关标签:
3条回答
  • 2020-12-20 02:31

    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.

    0 讨论(0)
  • 2020-12-20 02:48

    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.

    0 讨论(0)
  • 2020-12-20 02:53

    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)} }
    
    0 讨论(0)
提交回复
热议问题