如何在不截断值的情况下使用 Format-Table? [英] How do I use Format-Table without truncation of values?

查看:58
本文介绍了如何在不截断值的情况下使用 Format-Table?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个脚本可以 ping 服务器并检查每台服务器上运行的服务的状态.

I currently have a script that pings servers and checks the status of services running on each server.

我使用 Out-File 保存输出,但 PowerShell 在长字符串后放置省略号或...".我不希望它这样做.例如:

I save the output with Out-File but PowerShell places ellipses or "..." after long strings. I don't want it to do this. For example:

MachineName  ServiceName             Status StartType
-----------  -----------             ------ ---------
SrvGtw01     Test.MyService....       Running  

我希望它显示全名,如:

I want it to display the full name like:

MachineName  ServiceName              Status StartType
-----------  -----------              ------ ---------
SrvGtw01     Test.MyServiceName.Here  Stopped  Disabled

我一直在读到您可以将 $FormatEnumerationLimit 首选项变量设置为 -1,我已经尝试过,但它不起作用.我不确定我应该如何将它放在我的脚本中.

I've been reading that you can set the $FormatEnumerationLimit preference variable to -1 and I have tried that but it's not working. I'm not sure how I should place it in my script.

推荐答案

$FormatEnumerationLimit 偏好变量在这里不适用,因为它的目的是确定有多少要显示的 collection-valued 属性的元素(例如,$FormatEnumerationLimit = 2; [pscustomobject] @{ prop = 1, 2, 3 } 打印(最多)2.prop 值中的元素,并用 ... 提示存在更多元素;例如,{1, 2...}).

The $FormatEnumerationLimit preference variable doesn't apply here, because its purpose is to determine how many elements of a collection-valued property to display (e.g, $FormatEnumerationLimit = 2; [pscustomobject] @{ prop = 1, 2, 3 } prints (at most) 2 elements from .prop's value and hints at the existence of more with ...; e.g., {1, 2...}).

相反,您必须:

  • (a) 确保各个列不会在显示时截断其值:

  • (a) ensure that individual columns don't truncate their values on display:

  • 先管道到 Format-Table -Autosize.

(b) 确保整体输出宽度可以适合所有列:

and (b) ensure that the overall output width can fit all columns:

  • 管道到 Out-File -Width 具有足够大的值(不要使用 [int]::MaxValue,不过,因为表格输出的每一行都被填充到那个宽度[1]).

  • Pipe to Out-File -Width with a sufficiently large value (don't use [int]::MaxValue, though, because every line of tabular output gets padded to that very width[1]) .

警告: 如果您没有明确设置 -Width - 如果您只使用 > 会发生这种情况;,例如 - 使用当前控制台窗口的宽度 - 不管它是什么.

Caveat: If you don't set -Width explicitly - as would happen if you just used >, for instance - the current console window's width is used - whatever it happens to be.

例如:

# Assumes that the objects in $results only contain the properties
# of interest (MachineName, ServiceName, Status, StartType); you 
# can also pass an explicit list of output properties to Format-Table, however.
$results | Format-Table -AutoSize | Out-File -Width 512 C:\log.txt -Append

注意:要在控制台中预览输出 - 这可能涉及换行 - 使用
Out-String -Width 512 代替.

Note: To preview the output in the console - which may involve line-wrapping - use
Out-String -Width 512 instead.

[1] 在 PowerShell Core 中,这个不需要的最后一列填充已被删除,至少从 v6.1.0 开始.

[1] In PowerShell Core this undesirable last-column padding has been removed, as of at least v6.1.0.

这篇关于如何在不截断值的情况下使用 Format-Table?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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