gpt4 book ai didi

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

转载 作者:bug小助手 更新时间:2023-10-28 10:49:47 24 4
gpt4 key购买 nike

我目前有一个脚本可以 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 开始。

关于powershell - 如何在不截断值的情况下使用 Format-Table?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49122925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com