Author Posts

March 5, 2018 at 11:55 am

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

March 5, 2018 at 2:54 pm

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.

March 5, 2018 at 4:09 pm

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 !