Powershell: Autosize and Specific Column Width

好久不见. 提交于 2019-11-28 09:51:33

问题


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 the collection to table-view, the autosize would not allow me to specify specific column width.

For example:

id     name     items                                                          others
---    ----     -----                                                          -----
01     a        ("The first item", "The second item", "The third item", "...")  ...
02     ....

Here is my output:

$colObjs | Select-object id, name, items, others`
   @{Expression={($_.items -join "`n")};Label="Items"} | `
   Format-table -autosize -wrap

Then the items column would be the whole length of strings in its array:

id     name     items                                                          others
---    ----     -----                                                          -----
01     a        "The first item"                                               ...
                "The second item"                                              ....
                "The third item"
                ...
02     ....

I tried to use the following codes, still the width of items column not as expected:

$colObjs | Select-object id, name, items, others `
   @{Expression={($_.items -join "`n")};Label="Items"} | `
   Format-table id, name, `
     @{expression={$_.items}; label="items"; width=18}, others `
   -autosize -wrap

Specially, if the items have a long list of strings, the table view looks very ugly, too much spaces in column items.

This is the format what I want:

id     name     items              others
---    ----     -----              -----
01     a        "The first item"   ...
                "The second item"  ....
                "The third item"
                ...
02     ....

Is this true that -autosize would make width having no effect? How can I specify the width of items and leave other columns as auto-sized?


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/22723954/powershell-autosize-and-specific-column-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!