将PowerShell脚本更改为输出无省略号(...)的内容

37

我需要帮助,使以下脚本的输出不显示省略号(...)。 我尝试插入| Format-Table -Wrap -AutoSize ,但我似乎无法做到。

 clear-host Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue    
 $services = new-object system.collections.sortedlist
 $servers = (get-spfarm).servers  
 foreach ($server in $servers) {
     foreach($service in $server.serviceinstances)
     {
         if ($service.status = "Online")
         {
             $s = $service.typename
             if ($services.contains($s))
             {
                 $serverlist = $services[$s]
                 $servername = $server.name 
                 $services[$s]  = "$serverlist - $servername"
             }
             else
             {
                 $services[$s] = $server.name
             }
         }
     } } 
  $services

输出:

Name                            Value                                                                           
----                           -----                                                                           
Access Database Service        SE5APP - SE5FE - SE7FE - FAQ3                                          
Application Discovery **and L...** SE5APP - SE5FE - SE7FE - FAQ3                                          
Application Registry Service   SE5APP - SE5FE - SE7FE - FAQ3                                          

你能展示一下带有省略号的输出的例子吗? - David
3个回答

41

4
最终使用了| Format-Table -Wrap -AutoSize,但是因为你的帮助,我才明白在哪里插入代码。谢谢。 - MicroSumol
如果我想在包含“+类别信息”的输出中显示截断错误怎么办?我得到了“ResourceExists:(CN = tk,OU = Schwab ...”。这三个点很讨厌... - Timo
因此,您必须事先知道您的结果是列表还是表格。Get-service会返回一个列表,因此fl有效,但它看起来像一张表格。 - Timo

26

我看到了这篇文章并想要添加一些信息,因为接受的解决方案没有解决我的问题,我相信其他人可能会发现以下信息有用:

简要介绍:使用Microsoft Online Services ModulePowershell运行命令时,大部分结果都被截断,缺失的数据显示为省略号(...)。

解决方法:如Greig的这篇文章所述,我最终得出结论:$FormatEnumerationLimit=-1是问题的终极解决方案。使用任何变体的Format-WideFormat-ListFormat-TableFormat-Custom-AutoSizeOut-String -Width等,需要大量的额外考虑/代码。在所有你想要看到所有返回的数据的情况下,无论列、数组等如何,$FormatEnumerationLimit=-1确保你会得到 everything,而且你不需要麻烦。

额外信息,如Greig的文章中所述:

PowerShell快速提示:使用PowerShell创建宽表,作者解释道:

如果你有一个包含项目集合的特定属性,那么该属性在此处生成的文件中仍可能显示省略号,如果该集合中的项目数超过内置$FormatEnumerationLimit变量分配的数量。

根据所述,“将结果传递给| Format-Table -Property * [将]显示所有列。”但是,表格中的内容可能仍然被截断(“默认情况下,PowerShell截断表格输出”),即使使用| Format-Table -Property * -AutoSize也会受到屏幕缓冲区的限制(“自动调整大小的表格被限制为屏幕缓冲区的宽度”)。 在绝对值$ FormatEnumerationLimit = -1之前提供的解决方案似乎是同时使用| Format-Table -Property * -AutoSize| Out-String -Width 4096或您需要的任何宽度。

使用格式命令更改输出视图提供了有关格式命令的一些详细文档: Format-WideFormat-ListFormat-Table


3
这个命令“| Format-Table -Property * -AutoSize | Out-String -Width 4096”对我有效。 - GuruKay
@TryTryAgain,你帖子中的“使用格式命令更改输出视图”链接已经失效。这里是有效链接 - Mass Dot Net
@MassDotNet 感谢您指出这一点。旧的链接似乎已经失效了,所以我已经更改了链接,让它指向“回溯机器”。 - TryTryAgain

0
在这种情况下,我所做的是创建一个格式说明,然后将其用作我的Format-Table命令的参数。我开发了一个函数(Get-MaxLength)来检查具有最长数据的数据字段(在格式说明的末尾很有帮助),并使用它返回的值在格式说明中设置宽度。您可以在下面的代码中看到计算。请注意Intel(4)管理引擎接口的数字值。还要注意在Format-Table命令上使用-Wrap。该概念可以修改为计算所有字段宽度或仅计算最后一个字段的宽度,这只是一点数学。
Function Get-MaxLength {

<#
.SYNOPSIS
   Finds the length of the longest item in collection.

.DESCRIPTION
   Use this Function to get the length of the longest item in a
   collection for use in format strings or other places where
   needed.

.PARAMETER TestObj
    The qualified object to be tested. See example!

.Parameter MinLen
    The minimum length of the item (if using for formatting) which
    should be the Label (title) length. Note if the object item
    being tested does not have a Length property you MUST specify
    the label length!

.OUTPUTS
    Returns a numerical value

.EXAMPLE
   $NameLen = Get-MaxLength -TestObj $DotNet.PSChildName
   $VerLen  = Get-MaxLength -TestObj $DotNet.Version
   $RNLen   = Get-MaxLength -TestObj $DotNet.Release -MinLen 11

     #--- .Net Information ---

 $fmtDotNet =
  @{Expression={$_.PSChildName};Label=".Net Type";Width=$NameLen},
  @{Expression={$_.Version};Label="Version No:";Width=$VerLen},
  @{Expression={$_.Release};Label="Release No:";Width=$RNLen}

  $Dotnet | Format-Table $fmtDotNet
#>

  Param(
    [Parameter(Mandatory=$True)]
     [object] $TestObj,
    [Parameter(Mandatory=$False)]
     [int] $MinLen = 0,
    [Parameter(Mandatory=$False)]
     [int] $MaxLen = 0
  )

   $ErrorActionPreference = "SilentlyContinue"

   foreach ($x in $TestObj) {
     If ($x.Trim().length -gt $MinLen) {
       $MinLen = $x.Trim().length
     }
   }

   If ($MaxLen -ne 0) {
     If ($MinLen -gt $MaxLen) {
       $MinLen = $MaxLen
     }
   }

   $ErrorActionPreference = "Continue"

   Return ,$MinLen

} #End Function -----------  Get-MaxLength  -------------------

  $OstrWidth = 80
  
  $DriverInfo =
  Get-CimInstance -ClassName 'Win32_PNPSignedDriver'         |
  Where-Object -Property DriverProviderName  -ne "Microsoft" |
  Where-Object -Property DeviceName -ne -Value $Null         |
  Sort-Object  -Property DeviceName -Unique

$DriverCnt = $DriverInfo.Count

  $DVLen =
    Get-MaxLength -TestObj $DriverInfo.DriverVersion -MinLen 14
  $DDLen = $OstrWidth - $DVLen

  $fmtDRVR = @{Label="`nDriver Description";Width=$DDLen;
                                 Expression={$_.DeviceName}},
             @{Label="Version Number";    Width=$DVLen;
                                 Expression={$_.DriverVersion}}

  $DrvTitle = "$($DriverCnt) Non-Windows Unique Drivers and " +
              "Version Numbers:" | Out-String

  $DriverInfo =
    $DriverInfo | Format-Table -Property $fmtDRVR -Wrap |
                  Out-String   -Width $OStrWidth

样例输出:

Driver Description                                                 Number
-------------------                                                -------------
Alcor Micro USB 2.0 Card Reader                                    2.0.150.10135
ASMedia USB3.1 eXtensible Host Controller                          1.16.42.1
...
Intel(R) HD Graphics 630                                           21.20.16.4550
Intel(R) Management Engine Interface                               1914.12.0.125
                                                                   6
Intel(R) Ready Mode Technology Device                              1.2.0.0
...
Realtek Audio                                                      6.0.1.8248
Samsung NVMe Controller                                            3.0.0.1802

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接