Truncated Output

Welcome Forums General PowerShell Q&A Truncated Output

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
8 months, 2 weeks ago.

  • Author
    Posts
  • #95170

    Participant
    Points: 1
    Rank: Member

    I have a rather strange issue, when I use the following code to retrieve the PS version from a list of servers, the PS version is truncated. However this doesn't happen all the time, as when I query a short list of servers the output is ok.

    $pcs = Get-ADComputer -Filter "OperatingSystem -like 'Windows 2012 R2 Standard'" -Properties OperatingSystem
    
    foreach ($computer in $pcs)
           Try {
                 $psver=invoke-command -computername ($Computer.name) -ScriptBlock {$PSVersionTable.PSVersion.ToString();}
                 $ResultObject = New-Object PSObject -Property @{PSVer = $psver; ComputerName = ($Computer.name)}
                 $Resultobject
               }
           Catch
               {
                 Write-Host "Error: $($_.Exception.Message)
               }
    }
    

    Output:

    ComputerName   PSVer
    TEST01         4.0
    TEST02         4.0
    TEST03         5....
    

    I did also add $FormatEnumerationLimit = -1, didn;t seem to make any difference.

    Any help much appreciated.
    Thanks

  • #95178

    Keymaster
    Points: 1,624
    Helping HandTeam Member
    Rank: Community Hero

    That's just the formatting system doing best-guess stuff. I've had it happen to me as well in odd places.

    That's why I ALWAYS write bits bits this as a function. It makes it easier to pipe the function output to Format-Table or Format-List, where I can provide more specific formatting instructions. Remember, **your "main" code shouldn't worry about what the output looks like** . It should just output objects, as you're doing, and let the chips fall where they may. You can then take "external" steps to either explicitly format or, if you're really doing the right thing and building modules, define a custom format view for your output.

    • #95185

      Participant
      Points: 1
      Rank: Member

      Thanks Don, I've stuck it in a function and can now format it properly, I was thinking script, when I should have been thinking module !

The topic ‘Truncated Output’ is closed to new replies.