1

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?

1
  • $Columns = $ContentServeres.Keys | Sort-Object | Foreach-Object { @{Label=$_; Alignment='right'} } Commented Sep 29, 2017 at 18:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I've found the problem. I've collected columns definition with += and got hashtable instead of array. It should be += , used. I.e.

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

But there is another problem, Expression should be defined and I've not found yet how to do it dynamically...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.