Change powershell script to output without ellipses (…)

前端 未结 3 1374
野的像风
野的像风 2021-02-01 03:19

I need some help with the output of the following script so the output doesn\'t show with the ellipses (...). I tried to insert | Format-Table -Wrap -AutoSize but I

相关标签:
3条回答
  • 2021-02-01 04:03

    What I do in this situation is to create a format description then use that as an argument to my Format-Table command. I've developed a function (Get-MaxLength) to examine the data field with the longest data (helps to have this at the end of the format description) and set the width in the format description with the value it returns. You can see the calculations in the code below. Notice the Number value for the Intel(4) Management Engine Interface. Also notice the use of -Wrap on the Format-Table command. This concept can be modified to calculate all fields widths or just the last one, it's just a little math.

    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
    

    Sample Output:

    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
    
    0 讨论(0)
  • 2021-02-01 04:06

    Either Format-List (fl) or Format-Table -auto (ft -auto) should help here.

    $services | fl
    

    OR

    $services | ft -auto
    
    0 讨论(0)
  • 2021-02-01 04:19

    I came across this post and would like to add some information, as the accepted solution did not resolve my problem and I'm sure others may find the following information useful:

    Quick Story: Running commands using Microsoft Online Services Module with Powershell, much of the results were continually be retrieved as truncated with data cutoff and missing as an ellipsis (...).

    The fix: As explained in this post by Greig, I inevitably came to the conclusion $FormatEnumerationLimit=-1 is the unlimate solution to the problem. Using any variant of Format-Wide, Format-List, Format-Table, Format-Custom, -AutoSize, Out-String -Width, etc. require a hefty amount of additional considerations/code. In the case where all you want is to see all the data being returned, regardless of columns, arrays, etc., $FormatEnumerationLimit=-1 ensures you will get everything and you don't need to mess around.

    Additional information, as credited in Greig's post include:

    PowerShell Quick Tip: Creating wide tables with PowerShell, where the author explains:

    If you have a specific property that contains a collection of items, that property may still show an ellipsis in the file produced here if the number of items in that collection exceeds the number assigned to the built-in $FormatEnumerationLimit variable.

    ...and that "passing the results to | Format-Table -Property * [will] show all of the columns." But content from the columns may still be truncated ("PowerShell truncates table output by default"), and that even using | Format-Table -Property * -AutoSize will be limited by your screen buffer ("Auto-sized tables are limited to the width of your screen buffer"). The solution offered, before the absolute $FormatEnumerationLimit=-1, seems to be using | Format-Table -Property * -AutoSize in conjunction with | Out-String -Width 4096 or whatever width you require.

    Using Format Commands to Change Output View provides some more delailed documentation on the Format cmdlets: Format-Wide, Format-List, and Format-Table.

    0 讨论(0)
提交回复
热议问题