0

Output of format table
I am trying to use a PSCustomObject to store a bunch of information from a remote computer. I can't seem to get the output of Format-Table to work the way i want it.

As the picture shows, the list of items in the PSCustom object displays inside curly braces instead of as a list under the column header.

Below is the code I am using to generate the test PSCustomObject and populate one of the properties.

$EnvironmentInfo = [PSCustomObject] @{Name=[System.Collections.ArrayList]@(); Description=[System.Collections.ArrayList]@(); Publisher=[System.Collections.ArrayList]@(); Doggo=[System.Collections.ArrayList]@()}

$EnvironmentInfo.Name.Add("Doggo")
$EnvironmentInfo.Name.Add("Doggo")
$EnvironmentInfo.Name.Add("Doggo")
$EnvironmentInfo.Name.Add("Doggo")
$EnvironmentInfo.Name.Add("Doggo")
$EnvironmentInfo.Name.Add("Doggo")
$EnvironmentInfo.Name.Add("Doggo")
$EnvironmentInfo.Name.Add("Doggo")
$EnvironmentInfo.Name.Add("Doggo")

$EnvironmentInfo | Format-Table -Property $_
Fullmetal99012
  • 191
  • 3
  • 12
  • 1
    Why do you do it that way instead of `$EnvironmentInfo = @( [PSCustomObject]@{ Name = 'Name 1'; Description = 'Description 1'; Publisher = 'Publisher 1'; Doggo = 'Doggo 1'}; [PSCustomObject]@{ Name = 'Name 2'; Description = 'Description 2'; Publisher = 'Publisher 2'; Doggo = 'Doggo 2'} )`? – user4003407 Mar 08 '17 at 16:27
  • Please show your desired output. – Ansgar Wiechers Mar 08 '17 at 16:30
  • @PetSerAl I guess I could somehow pipe Get-Hotfix into an array of pscustomobject, just doesn't feel right – Fullmetal99012 Mar 08 '17 at 18:26
  • @PetSerAl Your solution works beautifully. I just need to finagle the data i'm retrieving into PSCustomObjects, but that should be simple. Thanks for the info! – Fullmetal99012 Mar 08 '17 at 18:33

1 Answers1

1

Your question is rather brief and leaves a lot to the imagination. However it looks like you want to gather an array of information from multiple sources and combine it in order to produce a neatly formatted output. I can provide an example which may (or may not) help you. The example collects HotFix info (per @PetSerAI) from several machines and returns an object for each hotfix which is piped into a Format-Table.

<#
.Synopsis
   Gather HotFix Info
.DESCRIPTION
   Gather HotFix Info from one or more computers
.EXAMPLE
   @("LCFSQL01","LCFSQL02","LCFSQL03","LCFSQL05") | Gather-HotFixInfo
   Gathers info for several remote machines
.EXAMPLE
   Gather-HotFixInfo -Machine = "LCFSQL01"
   Gathers info for a single remote machine
#>
Function Gather-HotFixInfo
{
    [CmdletBinding()]
    Param
    (
        # Machine remote machine name
        [Parameter(Mandatory=$true,
                   ValueFromPipeline=$true,
                   Position=0)]
        [string]$Machine
    )
    Process
    {
        Try
        {
            Get-HotFix -ComputerName $machine | ForEach-Object {
                [pscustomobject]@{Name=$_.CSName;
                    Description=$_.Description;
                    HotFixID=$_.HotFixID;
                    Doggo="Doggo"}
            }    
        }
        Catch
        {
            Write-Warning "Could not connect to $machine"
        }        
    }
}

# List of all computers from which to gather info
@("LCFSQL01","LCFSQL02","LCFSQL03","LCFSQL05") | Gather-HotFixInfo | Format-Table

Formatted Output

Chip Wood
  • 1,246
  • 1
  • 10
  • 16
  • Up voted mainly due to the insightful way of showing a bag of very useful Powershell aspects within a tiny piece of code: functions, documentation, parameter passing and some of their defining options, calling conventions, pscustomobject, try-catch, beautiful coding style accessible to beginners of this language. – ikaerom Jan 04 '22 at 14:49