1
votes

I have simplified the process so that anyone can reproduce it (in my script it is a problem within a workflow, no functions involved, therefore I can`t save these values in variables and then show them by console). The first thing is to use powershell ISE.


    function AAA {
        $datum1 = New-Object -TypeName PSObject
        $datum1 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
        $datum1 | Add-Member -MemberType NoteProperty -Name ColumnB -Value "BB"
        return $datum1
    }
    
    function BBB {
        $datum2 = New-Object -TypeName PSObject
        $datum2 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
        $datum2 | Add-Member -MemberType NoteProperty -Name ColumnC -Value "CC"
        $datum2 | Add-Member -MemberType NoteProperty -Name ColumnD -Value "DD"
        $datum2 | Add-Member -MemberType NoteProperty -Name ColumnE -Value "EE"
        $datum2 | Add-Member -MemberType NoteProperty -Name ColumnF -Value "FF"
        return $datum2
    }
    
    AAA
    BBB

Output:

    ColumnA ColumnB
    ------- -------
    AA      BB     
    AA             

Any idea why this happens?

EDIT: I have solved this "sh1t" in the following way


function AAA {
    $datum1 = New-Object -TypeName PSObject
    $datum1 | Add-Member -MemberType NoteProperty -Name Index -Value "1"
    $datum1 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
    $datum1 | Add-Member -MemberType NoteProperty -Name ColumnB -Value "BB"
    $datum1
}

function BBB {
    $datum2 = New-Object -TypeName PSObject
    $datum2 | Add-Member -MemberType NoteProperty -Name Index -Value "2"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnC -Value "CC"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnD -Value "DD"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnE -Value "EE"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnF -Value "FF"
    $datum2
}

AAA |? {$_.Index -eq "1"} | FT
BBB |? {$_.Index -eq "2"} | FT

Anyway, can someone explain what's going on? I would really like to understand the reason and look for related information

EDIT2: Reply to Palle Due,

it doesn't work at all for my scenario as it adds a bit more complexity. Out-Host is not an option. Example provided:


    workflow Get-Report
        {
            param ([string[]]$computername)
            foreach -Parallel ($computer in $computername) {
                sequence {
                    InlineScript {
                        $AAA = Invoke-Command -ScriptBlock {
                            $datum1 = New-Object -TypeName PSObject
                            $datum1 | Add-Member -MemberType NoteProperty -Name Index -Value "1"
                            $datum1 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
                            $datum1 | Add-Member -MemberType NoteProperty -Name ColumnB -Value "BB"
                            return $datum1
                        }
                        $AAA | Out-Host
        
                        $BBB = Invoke-Command -ScriptBlock {
                            $datum2 = New-Object -TypeName PSObject
                            $datum2 | Add-Member -MemberType NoteProperty -Name Index -Value "2"
                            $datum2 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
                            $datum2 | Add-Member -MemberType NoteProperty -Name ColumnC -Value "CC"
                            $datum2 | Add-Member -MemberType NoteProperty -Name ColumnD -Value "DD"
                            $datum2 | Add-Member -MemberType NoteProperty -Name ColumnE -Value "EE"
                            $datum2 | Add-Member -MemberType NoteProperty -Name ColumnF -Value "FF"
                            return $datum2
                        }
                        $BBB | Out-Host
                    } #endinlinescript
                } #endsequence
            } #endforeach
        } #endWF
        
        $results = @()
        $results = Get-Report -Verbose -Computer "localhost","localhost"
        $results | FT

1
I can't explain what's going on, but I have some observations. BBB has the right properties, you can see that if you evaluate BBB afterwards. Apparently only the common properties are shown. If you add a ColumnB property to $datum2 that gets shown as well. If you call BBB AAA, you get the expected output, but not as a table.Palle Due

1 Answers

0
votes

The easy fix is to pipe the output to Out-Host:

function AAA {
    $datum1 = New-Object -TypeName PSObject
    $datum1 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
    $datum1 | Add-Member -MemberType NoteProperty -Name ColumnB -Value "BB"
    return $datum1
}
    
function BBB {
    $datum2 = New-Object -TypeName PSObject
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnA -Value "AA"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnC -Value "CC"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnD -Value "DD"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnE -Value "EE"
    $datum2 | Add-Member -MemberType NoteProperty -Name ColumnF -Value "FF"
    return $datum2
}
    
AAA | Out-Host
BBB | Out-Host

gives the output:

ColumnA ColumnB
------- -------
AA      BB     

ColumnA : AA
ColumnC : CC
ColumnD : DD
ColumnE : EE
ColumnF : FF

The thing is that powershell likes to output custom objects with four members or less horizontally. With five or more members they are output vertically. And then some strange neatness priciple kicks in: If you output multiple objects in a row, powershell will try to keep the same format. So if you started out horizontally, it will keep going horizontally and only output members with the same name as the first custom object. If you start out vertically it will keep going vertically and output all names.

That's why if you go:

BBB
AAA

you will get

ColumnA : AA
ColumnC : CC
ColumnD : DD
ColumnE : EE
ColumnF : FF

ColumnA : AA
ColumnB : BB

instead.

I haven't been able to find any documentation for this. They're just my own observations.