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

Kad*_*ams 9 powershell formatting powershell-2.0

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

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

MachineName  ServiceName             Status StartType
-----------  -----------             ------ ---------
SrvGtw01     Test.MyService....       Running  
Run Code Online (Sandbox Code Playgroud)

我希望它显示如下全名:

MachineName  ServiceName              Status StartType
-----------  -----------              ------ ---------
SrvGtw01     Test.MyServiceName.Here  Stopped  Disabled
Run Code Online (Sandbox Code Playgroud)

我一直在阅读你可以设置$FormatEnumerationLimit偏好变量-1,我已经尝试过,但它没有用.我不确定如何将它放在我的脚本中.

mkl*_*nt0 6

$FormatEnumerationLimit偏好变量不适用于此,因为它的目的是确定的有多少个元素集合值属性来显示(例如,$FormatEnumerationLimit = 2; [pscustomobject] @{ prop = 1, 2, 3 }印刷品(至多)从2个元素.prop的值和提示以更与存在...;例如,{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
Run Code Online (Sandbox Code Playgroud)

注意:要在控制台中预览输出(可能涉及换行),请
Out-String -Width 512改用。


[1]在PowerShell Core中,至少从v6.1.0起,已删除了此多余的最后一栏填充。