使用格式表控制列宽 [英] Controlling column widths with Format-Table

查看:98
本文介绍了使用格式表控制列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从列表中获取某些PC的上次重启时间.当我使用

I am trying to get the last reboot time of some PCs from a list. When I use

foreach ($pc in $pclist) {
  Get-CimInstance -ClassName win32_operatingsystem -ComputerName $pc |
    select csname, lastbootuptime 
}

输出如下.

csname       lastbootuptime
------       --------------
CONFA7-L1-1A 7/15/2016 9:55:16 AM
CONFA7-L1-1F 5/31/2016 8:51:46 AM
CONFA7-L1-1G 6/18/2016 11:09:15 AM
CONFA7-L1... 6/26/2016 5:31:31 PM
CONFA7-L3... 7/24/2016 3:48:43 PM

这很整洁,但是如果PC名称很长,我将看不到全名. 所以我管道了Format-Table:

Which is neat, but if the PC name is long, I am unable to see the full name. So I pipelined Format-Table:

Get-CimInstance -ClassName win32_operatingsystem -ComputerName $pc |
  select csname, lastbootuptime |
  Format-Table  -HideTableHeaders 

这就是我得到的:

CONFA7-L1-1A 7/15/2016 9:55:16 AM



CONFA7-L1-1E 7/21/2016 12:58:16 PM



CONFA7-L1-1F 5/31/2016 8:51:46 AM

这里有两个问题.

  1. 没有标题.如果我删除-HideTableHeaders,则每个不需要的输出都会有标题.

  1. There is no heading. If I remove -HideTableHeaders there will be heading for every output which is not required.

之间有很多空白.

基本上,我只需要获得与第一个输出相似的输出,但不会截断全名.我该如何解决?

Basically I just need to get an output similar to the first one, but without truncating the full names. How can I fix these?

推荐答案

总结并补充 PetSerAl Ansgar Wiechers :

tl;博士

Get-CimInstance -ClassName win32_operatingsystem -ComputerName $pclist |
  Sort-Object CSName |
    Format-Table CSName, LastBootUpTime -AutoSize

-AutoSize是什么来确保CSName(计算机名称)列的宽度尽可能大,以便完整显示所有值.

-AutoSize is what ensures that the CSName (computer name) column is as wide as it needs to be to show all values in full.

Get-CimInstance采用计算机名称的数组,因此不需要循环.但是,由于并行查询目标计算机 ,返回对象的顺序通常与计算机名称的输入顺序匹配-这可以通过Sort-Object CSName调用进行纠正.

Get-CimInstance takes an array of computer names, so there's no need for a loop; however, since the target computers are queried in parallel, the order of objects returned will typically not match the input order of computer names - this is rectified with the Sort-Object CSName call.

控制单个列的宽度 :

To control the width of individual columns:

# Instead of a simple property name, 'prop1', pass a *hashtable*
# (@{ ... }`) with a 'width' entry to Format-Table
PS> [pscustomobject] @{ prop1='1234567890'; prop2='other' } |
       Format-Table -Property @{ e='prop1'; width = 5 }, prop2

prop1 prop2
----- -----
1234… other

注意:在 Windows PowerShell 中,您只会看到12...作为截断值,因为它使用 3个单独的.字符表示截断在PowerShell [Core] 6+中,此功能已改进为使用单个字符(水平椭圆,

Note: In Windows PowerShell, you'll see just 12... as the truncated value, because it uses 3 individual . characters to represent the truncation; in PowerShell [Core] 6+ this was improved to using a single character, (HORIZONTAL ELLIPSIS, U+2026).

请继续阅读以了解有关表格格式的更多信息.

从根本上讲,您的问题是关于如何控制表格输出的输出列宽度的,该宽度适用于 any cmdlet的输出.

At its core, your question is about how to control the output column width of tabular output, which applies to any cmdlet's output.

直接使用Format-Table cmdlet进行表格输出, Select-Object :Select-Object的目的是创建自定义对象,不格式化输出;如果此类对象(通常是没有预定义格式视图的任何类型的实例)碰巧没有 4个或更少的属性,则默认情况下它们在幕后使用Format-Table进行格式化(但您无法应用选项);否则,将隐式使用Format-List. 谢谢, PetSerAl .

Use the Format-Table cmdlet (directly) for tabular output, not Select-Object: the purpose of Select-Object is to create custom objects, not to format output; if such objects (generally, instances of any type without predefined formatting views) happen to haven 4 or fewer properties, they are by default formatted with Format-Table behind the scenes (but you don't get to apply options); otherwise, it is Format-List that is implicitly used. Thanks, PetSerAl.

  • Format-Table始终将输出行限制为可用屏幕宽度 ,这意味着:

  • Format-Table invariably limits output lines to the available screen width, which means:

  • 列可能根本无法打印.
  • 最后打印的列的值可能会被截断,而缺少的部分会由...表示(尽管请注意,所有打印的列可以都具有截断的值)
  • Columns may not get printed at all.
  • The last column that is printed may have its value truncated, with the missing part indicated by ... (though note that all printed columns can have truncated values).

如果要创建较长的行 ,请将Format-Table的输出通过管道传送到| Out-File -Width <int>| Out-String -Stream -Width <int>;请注意,如果将后者打印到屏幕上,则行会换行(但多余的换行符不会成为数据的一部分).

If you want to create longer lines, pipe Format-Table's output to | Out-File -Width <int> or | Out-String -Stream -Width <int>; note that if you print the latter to the screen, lines will wrap (but the extra line breaks won't be part of the data).

  • 注意事项:在 Windows PowerShell 上,请勿使用-Width ([int]::MaxValue),因为带有格式设置数据的类型的表格式数据无条件地用空格右填充到完整宽度,这会消耗输出文件中过多的内存/空间,甚至可能耗尽内存.在PowerShell Core 中,至少从v6.1起已修复此问题.

  • Caveat: On Windows PowerShell, do NOT use -Width ([int]::MaxValue), because table-formatted data for types with formatting data is unconditionally right-padded with spaces to the full width, which can consume inordinate amounts of memory / space in the output file and you may even run out of memory. In PowerShell Core, this has been fixed as of at least v6.1.

Windows 上的一种替代方法(在类似 Unix 的平台上的PowerShell Core中不起作用)是使用[console]::BufferWidth = <column-count>来扩大屏幕缓冲以允许较长的行不换行,但需要水平滚动.
此外,在Windows上,它只能在常规控制台中使用,而不能在ISE中使用.

An alternative on Windows (does not work in PowerShell Core on Unix-like platforms) is to use [console]::BufferWidth = <column-count> to widen the screen buffer to allow longer lines that don't wrap, but require horizontal scrolling.
Additionally, on Windows it only works in the regular console, not in the ISE.

控制列宽 -使用以下参数,间接确定要容纳多少列-

To control column widths - which indirectly determines how many columns will fit - use the following parameters:

  • -AutoSize ...告诉Format-Table使列尽可能宽以适合所有数据值,但是请注意,这样做可能会导致更少的列(通常更少:更多)列开始显示.

  • -AutoSize ... tells Format-Table to make columns as wide as necessary to fit all data values, but note that this can result in fewer (less typically: more) columns getting displayed.

-Wrap ...如果需要,可将 last 列的值跨多行显示,以避免被截断.

-Wrap ... makes the values of the last column printed span multiple lines, if needed, to avoid truncation.

指定自定义列宽 ,请将具有Width属性的哈希表作为元素传递给Format-Table' s -Property参数;例如,以下示例将第一个输出列限制为5个字符:
[pscustomobject] @{ prop1='1234567890'; prop2='other' } | Format-Table -Property @{ e='prop1'; width = 5 }, prop2
如果发生截断,截断指示器.../始终占据截断值( Windows PowerShell )的 last 3 个字符,只有 last 字符(PowerShell [Core] 6+);在上面的示例中,prop1值呈现为12.../1234…,总共5个字符.
另外,指定至少一个自定义宽度意味着您必须在-Property参数中显式枚举所有属性以输出,即使不需要自定义宽度的属性也是如此.

To specify custom column widths, pass a hashtable with a Width property as an element to Format-Table's -Property parameter; e.g., the following example limits the 1st output column to 5 characters:
[pscustomobject] @{ prop1='1234567890'; prop2='other' } | Format-Table -Property @{ e='prop1'; width = 5 }, prop2
If truncation occurs, the truncation indicator ... / invariably takes up the last 3 characters of the truncated value (Windows PowerShell), only the last character (PowerShell [Core] 6+); in the example above, the prop1 value renders as 12... / 1234… for a total of 5 chars.
Also, specifying at least one custom width means that you must explicitly enumerate all properties to output in the -Property argument, even the ones that don't need custom widths.

这篇关于使用格式表控制列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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