首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell中格式表cmdlet的最大列数是多少?

PowerShell中格式表cmdlet的最大列数是多少?
EN

Stack Overflow用户
提问于 2014-06-14 09:22:36
回答 3查看 9.2K关注 0票数 5

我正在使用格式-表格编写一个在列中发出输出的脚本,并且不能显示超过9(无论是否使用-AutoSize参数)。

以防万一这不是我的错,只是一些没有记录的东西:

http://technet.microsoft.com/en-us/library/hh849892.aspx可以显示的最大列数是多少?

如果没有绑定,我将从我的脚本中提取一个可复制的小案例。

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

代码语言:javascript
复制
    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
    })

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

编辑

根据建议 by Cole9350,我添加了论据来格式化表,但仍然失败:

代码语言:javascript
复制
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"
}

我从脚本的“主”部分调用此函数:

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

有关将-Wrap-Format-Table组合在一起的链接也表明,有时并不是所有的列都会显示,但不能说明在什么情况下。

所以我在寻找指导:当的时候,它不是全部展示给他们看吗?

我甚至重新排序了一些字段,并将HKCU/HKLM path合并为一个字段,并将其放在最后一个字段中:

代码语言:javascript
复制
    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
    })

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

代码语言:javascript
复制
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       ????      ????

随着路径更接近开头,它的输出为181个字符宽:

代码语言:javascript
复制
BDS # HKCU/HKLM registry path        Name                    CompilerVersion RTLVersion Define               Characterset Architectures                         Frameworks DllSuffix
----- -----------------------        ----                    --------------- ---------- ------               ------------ -------------                         ---------- ---------

对于-Wrap,但是没有-AutoSize,最后一列也不会显示。

Format-Table离开时,它确实显示了所有字段,因此New-Object PSCustomObject -Property确实生成了所有属性:

代码语言:javascript
复制
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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-05 16:48:12

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

(FYI:-Wrap用于显示列,但其中的数据被截断。)

例子:

代码语言:javascript
复制
$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 *

产出:

代码语言:javascript
复制
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  
票数 12
EN

Stack Overflow用户

发布于 2020-09-03 21:28:14

最简单和最可靠的方法是将$FormatEnumerationLimit -wrap一起使用。

事物是嵌套的时,Format-Table -wrap -Autosize仍然用...概括事物

使用嵌套对象测试它:

代码语言:javascript
复制
$hash = [pscustomobject]@{
   'List' = 0.. 100   
   'files' = (Get-ChildItem ~ ).Name
}

$hash | Format-Table -Wrap -AutoSize
$hash | Format-Table -AutoSize

产出:

代码语言:javascript
复制
List          files
----          -----
{0, 1, 2, 3…} {.android, .AndroidStudio, .AndroidStudio2.3, .beautifuldiscord…}


List          files
----          -----
{0, 1, 2, 3…} {.android, .AndroidStudio, .AndroidStudio2.3, .beautifuldiscord…}

展开前10项

代码语言:javascript
复制
$FormatEnumerationLimit = 10
$hash | Format-Table -AutoSize -Wrap

List                            files
----                            -----
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9…} {.android, .AndroidStudio, .AndroidStudio2.3,
                                .beautifuldiscord, .cache, .cargo, .config, .dbus-keyrings,
                                .dotnet, .fiddler…}

扩展所有的$FormatEnumerationLimit = -1

如果它太大,它就变得更难用了

代码语言:javascript
复制
List
----
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
94, 95, 96, 97, 98, 99, 100}

Format-List-1工作得更好

代码语言:javascript
复制
Get-Module | select Export* | Format-List

Format-Table15周围很好

超宽表输送到VS Code

代码语言:javascript
复制
$FormatEnumerationLimit = -1
Get-Module | format-table -AutoSize  | Out-String -Width 9999
票数 2
EN

Stack Overflow用户

发布于 2014-06-16 09:08:16

所以,现在您写了很多关于这个问题的文章,对我来说,格式表行为是由*.format.ps1xml文件在C:\Windows\System32\WindowsPowerShell\v1.0中给出的。但是我找不到PSObject,也找不到PSCustomObject (所以在默认情况下,9似乎是最大的)。

使用-property param of Format-Table,您可以按所需的顺序显示任意数量的属性。

也许您可以使用有关主题的Microsoft文档编写Windows PowerShell格式文件构建您的一种格式文件。

我处理问题的方法是创建一个My C#类( type ),使用Add插入它,然后提供一个类型文件和一个格式文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24218482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档