Output data from multiple piped cmdlets and format it into table

Say I have a number of piped PowerCLI cmdlets:

Get-VM | Get-VMHost | Get-Portgroups

How can I select the columns from each level of the command e.g:

Select Get-VM.Name, Get-VMHost.ID, Get-VMHost.Memory, Get-PortGroups.ID

Note: It is likely that different cmdlets will return identically named columns.

It is also likely that each cmdlet may return multiple lines of data e.g. a VMHost might have multiple VMs associated with it - I want to display this data in a table as follows (I doubt its possible but if columns/rows can use spanning to make the table more presentable even better):

HOST1 | VM1 | PortGroup1
HOST1 | VM1 | PortGroup2
HOST1 | VM1 | PortGroup3
HOST2 | VM2 | PortGroup1
HOST2 | VM2 | PortGroup2
HOST2 | VM3
Blowfelt82Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
When you need to intercept data in the pipeline so you can use it later on, usually you will use ForEach-Object and capture what you want into variables.  Then you can reference the variables as needed.  You can use Select-Object (could also use New-Object within a loop) with calculated properties to display/output all the information you want.
Get-VM | ForEach `
{
    $VMname = $_.name
    $_ | Get-VMHost | ForEach `
    {
        $HostID = $_.ID
        $HostMem = $_.Memory
        $_ | Get-Portgroups |
         Select @{n="Host";e={$HostID}},
                @{n="Memory";e={$HostMem}},
                @{n="VMName";e={$VMname}},
                @{n="PortGroupID";e={$_.ID}}
    }
}

Open in new window


You can add a pipe at the end to Format-Table (try using the -Autosize and -Wrap parameters to see if that gives you what you want).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.