Powershell:自动调整大小和特定列宽 [英] Powershell: Autosize and Specific Column Width

查看:151
本文介绍了Powershell:自动调整大小和特定列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于这个 SO 问题 Powershell:对象的输出集合,我能够将一组字符串包装在多行中的一列.但是,当我尝试将集合输出到 table-view 时,自动调整大小不允许我指定特定的列宽.

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.

例如:

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

这是我的输出:

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

然后 items 列将是其数组中字符串的整个长度:

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     ....

我尝试使用以下代码,但 items 列的宽度仍然不如预期:

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

特别是,如果items有很长的字符串列表,table view看起来会很丑,items列中的空格太多.

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

这是我想要的格式:

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

-autosize 会使宽度无效,这是真的吗?如何指定 items 的宽度并将其他列保留为自动调整大小?

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?

推荐答案

这里的方法略有不同.循环遍历您的集合,为每个集合找到每个属性的计数并选择最高计数.通过那么多循环运行该集合,并在每个循环中创建一个自定义对象,其中每个属性检查它是否是一个数组.如果是,则迭代到该数组中,如果不是,则检查这是否是第一轮自定义对象并返回值,否则返回空白.输出就是您要查找的内容,并且 -AutoSize for FT 可以完美运行.

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

它给了我这个输出:

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 

好的,更新了我的代码.它不再关心你扔给它什么数组,它有多少属性,或者这些属性有多少属性.只要给定的集合只包含字符串和/或数组,它就会输出所需的对象集合,就像我上面提到的输出一样.

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.

这篇关于Powershell:自动调整大小和特定列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆