Отступ для вывода таблицы формата в сценариях PowerShell - PullRequest
0 голосов
/ 01 марта 2011

Как сделать отступ из командлета Format-Table в определенный столбец?

У меня есть:

> $SomeValues | Format-Table -HideTableHeaders
A    1                
B    2
C    3

Но я бы хотел:

    A    1                
    B    2
    C    3

Ответы [ 6 ]

5 голосов
/ 01 марта 2011

Спасибо всем за ваши ответы. Они помогли мне понять, как сделать то, что я хотел, используя вычисленные свойства . Expression должно быть на единицу меньше отступа из-за автоматического пробела в один символ между столбцами в таблице.

Если вы используете флаг -AutoSize:

Write-Host "Not indented"
Write-Host "    Indented"
$a = @{ Aa = 1; Bbb = 2; Cccc = 300}
$a | Format-Table -Property @{Expression="   "},Name,Value -AutoSize -HideTableHeaders

Если вы не используете флаг -AutoSize:

Write-Host "Not indented"
Write-Host "    Indented"
$a = @{ Aa = 1; Bbb = 2; Cccc = 300}
$a | Format-Table -Property @{Expression={}; Width=3},Name,Value -HideTableHeaders

Вывод выглядит так:

Not indented
    Indented

    Bbb      2
    Aa       1
    Cccc   300
1 голос
/ 01 марта 2011
PS> $a= @{A=1;B=2;C=3}
PS> $a.GetEnumerator()|%{ "{0,10}{1,5}" -f $_.key,$_.value}
         A    1
         B    2
         C    3
PS>

0 голосов
/ 08 сентября 2018

Это должно сделать это

 function Indent-ConsoleOutput($output, $indent=4){
    if(!($output -eq $null)){
        if(!( $indent -is [string])){
            $indent = ''.PadRight($indent)
        }
        $width = (Get-Host).UI.RawUI.BufferSize.Width - $indent.length
        ($output| out-string).trim().replace( "`r", "").split("`n").trimend()| %{
            for($i=0; $i -le $_.length; $i+=$width){
                if(($i+$width) -le $_.length){ 
                    "$indent"+$_.substring($i, $width)
                }else{
                    "$indent"+$_.substring($i, $_.length - $i)
                }
            }
        }
    }
}

'##  Get-Process'
    Indent-ConsoleOutput ((get-process)[0..5]|format-table) 4
''
'## Log Stye Output'
    Indent-ConsoleOutput ((get-process)[0..5]|format-table) "    $(Get-Date) "
0 голосов
/ 22 августа 2018

Для общего решения

 function Indent-ConsoleOutput($output, $indent=4){
    if(!($output -eq $null)){
        if(!( $indent -is [string])){
            $indent = ''.PadRight($indent)
        }
        $width = (Get-Host).UI.RawUI.BufferSize.Width - $indent.length
        ($output| out-string).trim().replace( "`r", "").split("`n").trimend()| %{
            for($i=0; $i -le $_.length; $i+=$width){
                if(($i+$width) -le $_.length){ 
                    "$indent"+$_.substring($i, $width)
                }else{
                    "$indent"+$_.substring($i, $_.length - $i)
                }
            }
        }
    }
}

'##  Get-Process'
Indent-ConsoleOutput ((get-process)[0..5]|format-table) 4
''
'## Log Stye Output'
Indent-ConsoleOutput ((get-process)[0..5]|format-table) "    $(Get-Date) "
0 голосов
/ 01 марта 2011

На самом деле, вы можете использовать ft и -autosize

 $a= @{A=1;B=2;C=3}
 $a.getenumerator() | ft blah,name,value -hidetableheaders -auto

      A        1
      B        2
      C        3
0 голосов
/ 01 марта 2011

Как насчет $SomeValues | Format-Table -HideTableHeaders -Auto?

...