简体   繁体   English

PowerShell中的格式表:动态配置列

[英]Format-Table in PowerShell: dynamically configure columns

I try to format two dimensional table in PowerShell: 我尝试在PowerShell中格式化二维表:

$ContentServeres.Keys | Sort-Object | Foreach-Object {
    $Columns += @{Label=$_; Alignment='right'}
}

$ClientServeres.Keys | Sort-Object | Foreach-Object {
    PROCESS {
        $obj = $ClientServeres.Get_Item($_)

        $serverOutObj = New-Object PSObject
        $serverOutObj | Add-Member NoteProperty Client ("$($_)`t`t")

        $ContentServeres.Keys | Sort-Object | Foreach-Object {
            $serv = $obj.Get_Item($_)
            $serverOutObj | Add-Member NoteProperty $_ ("{0:N0}" -f $serv.SumLength)
        }
        Write-Output $serverOutObj
    }
} | Format-Table $Columns -AutoSize

But it doesn't work. 但这是行不通的。 I get error with $Columns: 我收到$ Columns错误:

InvalidArgument: (:) [Format-Table], NotSupportedException

I've found a Sample https://technet.microsoft.com/de-de/library/ee692794.aspx 我找到了一个样本https://technet.microsoft.com/de-de/library/ee692794.aspx

$a = @{Label="ColA"; Alignment='right'}, @{Label="ColB"; Alignment='right'}
...
Format-Table @a ...

How can I dynamically create such $a list? 如何动态创建这样的$ a列表?

I think you're trying to pipe data into Format-Table and also give columns as parameters. 我认为您正在尝试将数据传递到Format-Table并同时将列作为参数。

Put the Format-Table on a new line w/o piping data. 将格式表放在不带管道数据的新行中。

I've found the problem. 我发现了问题。 I've collected columns definition with += and got hashtable instead of array. 我已经用+ =收集了列定义,并得到了哈希表而不是数组。 It should be += , used. 应该使用+ =。 Ie

    $Columns += ,@{Label=$_; Alignment='right'}

But there is another problem, Expression should be defined and I've not found yet how to do it dynamically... 但是还有另一个问题,应该定义表达式,但我还没有找到如何动态地执行它的方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM