powershell - 如何在不截断值的情况下使用 Format-Table?

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

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

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

我希望它显示全名,如:

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

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

最佳答案

$FormatEnumerationLimit 首选项变量不适用于此处,因为它的目的是确定 collection-valued 属性的元素数量显示(例如,$FormatEnumerationLimit = 2; [pscustomobject] @{ prop = 1, 2, 3 }.prop 的值中打印(最多)2 个元素并通过 ... 提示存在更多内容;例如,{1, 2...})。

相反,您必须:

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

    • 先管道到Format-Table -Autosize
  • (b)确保整体输出宽度可以适合所有列:

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

    • 警告: 如果您没有明确设置 -Width - 如果您只使用 > ,例如 - 使用当前控制台窗口的宽度 - 不管它是什么。

例如:

# 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 代替。


[1] 在 PowerShell Core 中,这种不受欢迎的最后一列填充已被删除,至少从 v6.1.0 开始。

https://stackoverflow.com/questions/49122925/

相关文章:

java - 格式化相同数据的方法有很多

c# - String.Format 没有正确转换阿拉伯语的整数

css - 防止eclipse破坏CSS格式

android - EditText 提示随重力消失

javascript - 如何在 Eclipse 的 Javascript 源代码中设置数组元素的缩

android - 如何在 Intellij IDEA 13 for android 中按字母顺序对

c# - 支持自定义数字类型的 ToString(字符串格式)

php - 为什么 PHP 回显的文本会丢失其格式?

java - 如何将字符串解析为 java.sql.date

javascript - Number().toLocaleString() 在不同的浏览器中有不同