Powershell:自动调整大小和特定列宽

Powershell:自动调整大小和特定列宽,powershell,powershell-2.0,Powershell,Powershell 2.0,基于这个SO问题,我能够将字符串集合包装在一列中的多行中。但是,当我尝试将集合输出到表视图时,自动调整大小将不允许我指定特定的列宽 例如: id name items others --- ---- ----- ----- 01 a

基于这个SO问题,我能够将字符串集合包装在一列中的多行中。但是,当我尝试将集合输出到表视图时,自动调整大小将不允许我指定特定的列宽

例如:

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
列将是其数组中字符串的整个长度:

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 id, name, `
     @{expression={$_.items}; label="items"; width=18}, others `
   -autosize -wrap
特别是,如果
有一长串字符串,则表视图看起来非常难看,列
中的空格太多

这是我想要的格式:

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

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

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

首先,我制作了一个与您类似的收藏:

$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'})
    )
然后我通过我建议的代码运行了这一点:

$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 

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

我明白你的意思,我在控制台中没有答案,但如果你这样做是为了将其发送到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
(我修剪了“Expression”和“Label=”以使它们适合屏幕)

然后GridView显示具有正确宽度的列

既然GridView可以,为什么不能格式化表呢?也许你可以通过自定义视图来解决这个问题,我还没有尝试过这种方法。

这里是一个例子

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

看起来很有效。顺便问一下,代码中的{是什么?我见过{和%{,但没有{。顺便问一下,{和{是什么意思?{}是ForEach{}的缩写,{}是哈希表的构造函数。