简体   繁体   中英

Format-Table in PowerShell: dynamically configure columns

I try to format two dimensional table in 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:

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

I've found a Sample 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?

I think you're trying to pipe data into Format-Table and also give columns as parameters.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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