PowerShell 中 Format-Table cmdlet 的最大列数是多少 [英] What's the maximum number of columns for Format-Table cmdlet in PowerShell

查看:42
本文介绍了PowerShell 中 Format-Table cmdlet 的最大列数是多少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Format-Table,并且不能显示超过 9 个(无论是否带有 -AutoSize 参数).

以防万一这不是我的错,只是一些未记录的事情:

Format-Table 可以显示吗?

如果它是未绑定的,我会从我的脚本中提取一个小的可重现的案例.

我正在使用 PowerShell 4.0,并创建要列出的对象,如下所示:

 New-Object PSCustomObject -Property ([Ordered] @{"BDS #" = $bdsVersion"HKCU" = $hkcuBasePathHKLM" = $hklmBasePath姓名"= $fullName"编译器版本" = $compilerVersion"RTLVersion" = $rtlVersion定义"= $define"字符集" = $characterSet架构"= $架构框架" = $frameworkDllSuffix" = $dllSuffix})

即使我的控制台窗口足够宽(300 个字符宽),DllSuffix 列也不会显示.

编辑:

根据 Cole9350 的 comment37399115_24218482">建议,我添加了 -Wrap-AutoSize 参数 格式化表格,但它仍然失败:

function Get-BDS-ProductSummaries {$bdsVersions = Get-BDS-Versions$bdsVersions |ForEach-Object {$summary = Get-BDS-ProductSummary $_$summary} |Format-Table -AutoSize -Wrap# http://blogs.technet.com/b/nexthop/archive/2011/03/21/psformatorselect.aspx写主机斜线分隔值,如 xxx/yyy 表示本机/.NET"}

我从脚本的主要"部分调用这个函数:

$args |ForEach-Object {$arg = $_开关($arg){# ...'产品摘要'{编写主持人产品摘要:"Get-BDS-ProductSummaries}# ...}$anyArg = $True}

关于将 -Wrap-Format-Table 结合使用的解释链接也表明有时并非所有列都会显示,但不会说明在什么情况下.

所以我正在寻找指导:什么时候它没有显示所有内容?

我什至对一些字段重新排序,并将 HKCU/HKLM 路径合并为一个字段并将其放在最后:

 New-Object PSCustomObject -Property ([Ordered] @{"BDS #" = $bdsVersion姓名"= $fullName"编译器版本" = $compilerVersion"RTLVersion" = $rtlVersion定义"= $define"字符集" = $characterSet架构"= $架构框架" = $frameworkDllSuffix" = $dllSuffix项目版本"= $项目版本# "HKCU" = $hkcuBasePath# "HKLM" = $hklmBasePathHKCU/HKLM 注册表路径"= $basePath})

它显示了一个 165 个字符宽的表格(而我的控制台窗口是 300 个字符宽):

产品概要:BDS # Name CompilerVersion RTLVersion 定义字符集架构框架 DllSuffix ProjectVersion----- ---- --------------- ---------- ------ ------------ ------------- ---------- --------- --------------1 Borland C# Builder 1 C# C# C# Unicode C# .NET 1 .NET ???????2 Borland Delphi 8 none/16.0 none/16.0 VER160/VER160 Ansi/Unicode Win32;.NET 1 VCL/.NET 80 803 Borland Delphi 2005 17.0/17.0 17.0/17.0 VER170/VER170 Ansi/Unicode Win32;.NET 1 VCL/.NET 90 ????4 Borland Delphi 2006 18.0/18.0 18.0/18.0 VER180/VER180 Ansi/Unicode Win32;.NET 2 VCL/.NET 100 ????5 Borland Delphi 2007 18.5/19.0 18.0/19.0 VER180&VER185/VER190 Ansi/Unicode Win32;.NET 2 VCL/.NET 100 ????6 CodeGear Delphi 2009 20.0 20.0 VER200 Unicode Win32 VCL 120 11.1;12.07 CodeGear Delphi 2010 21.0 21.0 VER210 Unicode Win32 VCL 140 12.08 Embarcadero Delphi XE 22.0 22.0 VER220 Unicode Win32 VCL 150 12.2;12.39 Embarcadero Delphi XE2 23.0 23.0 VER230 Unicode Win32;Win64 VCL 160 13.410 Embarcadero Delphi XE3 24.0 24.0 VER240 Unicode Win32;Win64;OSX32 VCL;FMX1 170 14.3;14.411 Embarcadero Delphi XE4 25.0 25.0 VER250 Unicode Win32;Win64;OSX32;iOS-Arm VCL;FMX2 180 14.612 Embarcadero Delphi XE5 26.0 26.0 VER260 Unicode Win32;Win64;OSX32;iOS-Arm;Android-Arm VCL;FMX2 190 15.113 Embarcadero Appmethod 1 ??????????统一码???FMX2???????14 Embarcadero Delphi XE6 27.0 27.0 VER270 Unicode Win32;Win64;OSX32;iOS-Arm;Android-Arm VCL;FMX2 200 15.415 Embarcadero Appmethod 2 ??????????统一码???FMX2???????

路径越靠近开头,输出181个字符宽:

BDS # HKCU/HKLM 注册表路径名称 CompilerVersion RTLVersion 定义字符集架构框架 DllSuffix----- ----------------------- ---- --------------- ---------- ------ -------------- ------------- ---------- ---------

使用 -Wrap,但没有 -AutoSize,最后一列也不显示.

当离开 Format-Table 时,它会显示所有字段,所以 New-Object PSCustomObject -Property 会生成所有属性:

BDS # : 14HKCU/HKLM 注册表路径:\Software\Embarcadero\BDS\14.0名称 : Embarcadero Delphi XE6编译器版本:27.0RTL版本:27.0定义:VER270字符集:Unicode架构 : Win32;Win64;OSX32;iOS-Arm;Android-Arm框架:VCL;FMX2DLL后缀:200项目版本:15.4

解决方案

默认情况下,Format-Table 只会显示 10 列.要获得它们,请使用*".有关详细信息,请参阅下面的示例和输出.

(仅供参考:-Wrap 在列显示但其中的数据被截断时使用.)

示例:

$aryTemp = @()$objTemp = 新对象 PSObject$objTemp |Add-Member -type NoteProperty -Name Column1 -Value "Data1"$objTemp |Add-Member -type NoteProperty -Name Column2 -Value "Data2"$objTemp |Add-Member -type NoteProperty -Name Column3 -Value "Data3"$objTemp |Add-Member -type NoteProperty -Name Column4 -Value "Data4"$objTemp |Add-Member -type NoteProperty -Name Column5 -Value "Data5"$objTemp |Add-Member -type NoteProperty -Name Column6 -Value "Data6"$objTemp |Add-Member -type NoteProperty -Name Column7 -Value "Data7"$objTemp |Add-Member -type NoteProperty -Name Column8 -Value "Data8"$objTemp |Add-Member -type NoteProperty -Name Column9 -Value "Data9"$objTemp |Add-Member -type NoteProperty -Name Column10 -Value "Data10"$objTemp |Add-Member -type NoteProperty -Name Column11 -Value "Data11"$objTemp |Add-Member -type NoteProperty -Name Column12 -Value "Data12"$objTemp |Add-Member -type NoteProperty -Name Column13 -Value "Data13"$objTemp |Add-Member -type NoteProperty -Name Column14 -Value "Data14"$objTemp |Add-Member -type NoteProperty -Name Column15 -Value "Data15"$aryTemp += $objTemp#只显示10列$aryTemp |格式表#显示所有$aryTemp |格式表 *

输出:

Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9 Column10------- ------- ------- ------- ------- ------- ------- ------- ------- --------Data1 Data2 Data3 Data4 Data5 Data6 Data7 Data8 Data9 Data10Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9 Column10 Column11 Column12 Column13 Column14 Column15------- ------- ------- ------- ------- ------- ------- ------- ------- -------- -------- -------- -------- -------- --------Data1 Data2 Data3 Data4 Data5 Data6 Data7 Data8 Data9 Data10 Data11 Data12 Data13 Data14 Data15

I'm writing a script emitting output in columns using Format-Table, and cannot get more than 9 to show (either with or without the -AutoSize argument).

Just in case it is not my fault, just something undocumented:

What's the maximum number of columns which Format-Table can show?

If it is unbound, I'll distill a small reproducible case from my script.

I'm using PowerShell 4.0, and create the objects to list like this:

    New-Object PSCustomObject -Property ([Ordered] @{
        "BDS #" = $bdsVersion
        "HKCU" = $hkcuBasePath
        "HKLM" = $hklmBasePath
        "Name" = $fullName
        "CompilerVersion" = $compilerVersion
        "RTLVersion" = $rtlVersion
        "Define" = $define
        "Characterset" = $characterSet
        "Architectures" = $architecture
        "Frameworks" = $framework
        "DllSuffix" = $dllSuffix
    })

The DllSuffix column doesn't show even though my console window is wide enough (300 characters wide).

Edit:

As per suggestion by Cole9350, I added the -Wrap to the -AutoSize argument to format the table, but it still fails:

function Get-BDS-ProductSummaries {
    $bdsVersions = Get-BDS-Versions
    $bdsVersions | ForEach-Object { 
        $summary = Get-BDS-ProductSummary $_
        $summary
    } | Format-Table -AutoSize -Wrap
    # http://blogs.technet.com/b/nexthop/archive/2011/03/21/psformatorselect.aspx
    Write-Host "Slash separated values like xxx/yyy means native/.NET"
}

I call this function from my "main" portion of the script:

$args | ForEach-Object {
    $arg = $_
    switch ($arg) {
# ...
        'ProductSummaries' {
            Write-Host "Product Summaries:"
            Get-BDS-ProductSummaries
        }
# ...
    }
    $anyArg = $True
}

The link explaining about combining -Wrap with -Format-Table also indicates that sometimes not all columns will show, but doesn't tell under what circumstances.

So I'm looking for guidance: when doesn't it show them all?

I even reordered some of the fields, and merged the HKCU/HKLM path into one field and put that last:

    New-Object PSCustomObject -Property ([Ordered] @{
        "BDS #" = $bdsVersion
        "Name" = $fullName
        "CompilerVersion" = $compilerVersion
        "RTLVersion" = $rtlVersion
        "Define" = $define
        "Characterset" = $characterSet
        "Architectures" = $architecture
        "Frameworks" = $framework
        "DllSuffix" = $dllSuffix
        "ProjectVersion" = $projectVersion
    #    "HKCU" = $hkcuBasePath
    #    "HKLM" = $hklmBasePath
        "HKCU/HKLM registry path" = $basePath
    })

It shows a 165 character wide table (whereas my console Window is 300 characters wide):

Product Summaries:

BDS # Name                    CompilerVersion RTLVersion Define               Characterset Architectures                         Frameworks DllSuffix ProjectVersion
----- ----                    --------------- ---------- ------               ------------ -------------                         ---------- --------- --------------
    1 Borland C# Builder 1    C#              C#         C#                   Unicode      C# .NET 1                             .NET       ????      ????
    2 Borland Delphi 8        none/16.0       none/16.0  VER160/VER160        Ansi/Unicode Win32;.NET 1                          VCL/.NET   80        80
    3 Borland Delphi 2005     17.0/17.0       17.0/17.0  VER170/VER170        Ansi/Unicode Win32;.NET 1                          VCL/.NET   90        ????
    4 Borland Delphi 2006     18.0/18.0       18.0/18.0  VER180/VER180        Ansi/Unicode Win32;.NET 2                          VCL/.NET   100       ????
    5 Borland Delphi 2007     18.5/19.0       18.0/19.0  VER180&VER185/VER190 Ansi/Unicode Win32;.NET 2                          VCL/.NET   100       ????
    6 CodeGear Delphi 2009    20.0            20.0       VER200               Unicode      Win32                                 VCL        120       11.1;12.0
    7 CodeGear Delphi 2010    21.0            21.0       VER210               Unicode      Win32                                 VCL        140       12.0
    8 Embarcadero Delphi XE   22.0            22.0       VER220               Unicode      Win32                                 VCL        150       12.2;12.3
    9 Embarcadero Delphi XE2  23.0            23.0       VER230               Unicode      Win32;Win64                           VCL        160       13.4
   10 Embarcadero Delphi XE3  24.0            24.0       VER240               Unicode      Win32;Win64;OSX32                     VCL;FMX1   170       14.3;14.4
   11 Embarcadero Delphi XE4  25.0            25.0       VER250               Unicode      Win32;Win64;OSX32;iOS-Arm             VCL;FMX2   180       14.6
   12 Embarcadero Delphi XE5  26.0            26.0       VER260               Unicode      Win32;Win64;OSX32;iOS-Arm;Android-Arm VCL;FMX2   190       15.1
   13 Embarcadero Appmethod 1 ????            ????       ????                 Unicode      ????                                  FMX2       ????      ????
   14 Embarcadero Delphi XE6  27.0            27.0       VER270               Unicode      Win32;Win64;OSX32;iOS-Arm;Android-Arm VCL;FMX2   200       15.4
   15 Embarcadero Appmethod 2 ????            ????       ????                 Unicode      ????                                  FMX2       ????      ????

With the path more towards the beginning, the output it 181 characters wide:

BDS # HKCU/HKLM registry path        Name                    CompilerVersion RTLVersion Define               Characterset Architectures                         Frameworks DllSuffix
----- -----------------------        ----                    --------------- ---------- ------               ------------ -------------                         ---------- ---------

With -Wrap, but without -AutoSize the last column doesn't show either.

When leaving the Format-Table away, it does show all fields, so New-Object PSCustomObject -Property does produce all properties:

BDS #                   : 14
HKCU/HKLM registry path : \Software\Embarcadero\BDS\14.0
Name                    : Embarcadero Delphi XE6
CompilerVersion         : 27.0
RTLVersion              : 27.0
Define                  : VER270
Characterset            : Unicode
Architectures           : Win32;Win64;OSX32;iOS-Arm;Android-Arm
Frameworks              : VCL;FMX2
DllSuffix               : 200
ProjectVersion          : 15.4

解决方案

By default, Format-Table will only show 10 columns. To get them all, use "*". See Example and Output below for details.

(FYI: -Wrap is used when the column is being displayed but the data in it is being truncated.)

EXAMPLE:

$aryTemp = @()
$objTemp = New-Object PSObject
$objTemp | Add-Member -type NoteProperty -Name Column1 -Value "Data1"
$objTemp | Add-Member -type NoteProperty -Name Column2 -Value "Data2"
$objTemp | Add-Member -type NoteProperty -Name Column3 -Value "Data3"
$objTemp | Add-Member -type NoteProperty -Name Column4 -Value "Data4"
$objTemp | Add-Member -type NoteProperty -Name Column5 -Value "Data5"
$objTemp | Add-Member -type NoteProperty -Name Column6 -Value "Data6"
$objTemp | Add-Member -type NoteProperty -Name Column7 -Value "Data7"
$objTemp | Add-Member -type NoteProperty -Name Column8 -Value "Data8"
$objTemp | Add-Member -type NoteProperty -Name Column9 -Value "Data9"
$objTemp | Add-Member -type NoteProperty -Name Column10 -Value "Data10"
$objTemp | Add-Member -type NoteProperty -Name Column11 -Value "Data11"
$objTemp | Add-Member -type NoteProperty -Name Column12 -Value "Data12"
$objTemp | Add-Member -type NoteProperty -Name Column13 -Value "Data13"
$objTemp | Add-Member -type NoteProperty -Name Column14 -Value "Data14"
$objTemp | Add-Member -type NoteProperty -Name Column15 -Value "Data15"
$aryTemp += $objTemp

#only shows 10 columns
$aryTemp | Format-Table

#show all
$aryTemp | Format-Table *

OUTPUT:

Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9 Column10
------- ------- ------- ------- ------- ------- ------- ------- ------- --------
Data1   Data2   Data3   Data4   Data5   Data6   Data7   Data8   Data9   Data10  



Column1 Column2 Column3 Column4 Column5 Column6 Column7 Column8 Column9 Column10 Column11 Column12 Column13 Column14 Column15
------- ------- ------- ------- ------- ------- ------- ------- ------- -------- -------- -------- -------- -------- --------
Data1   Data2   Data3   Data4   Data5   Data6   Data7   Data8   Data9   Data10   Data11   Data12   Data13   Data14   Data15  

这篇关于PowerShell 中 Format-Table cmdlet 的最大列数是多少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆