2

I have a collection of objects, each one with one property called as ProductName. I also defined a function to get parts for a product:

function GetParts([string] ProductName))
{
   $parts = @()
   ....
   return $parts
}

What I need is to use pipe to loop each object and add "Parts" as another property to the object by calling my function, and finally output the collection to a table view:

$products | Sort-Object ProductName | Select-Object `
  -Property ProductName, `
            @{Expression=(GetParts $_.ProductName); Label="Parts"} | `
          Format-Table ProductName, Parts

I put a break point in my function, but I don't get debug stop. Not sure how to use pipe for a collection with my function to add a dynamic property (Parts).

5
  • So it looks like you want the function to output an object with 2 properties, the first being a string ProductName, and the second being an array of strings Parts. Is that right? Or do you want it to output an array of objects that have 2 properties, one being a string for the ProductName and then each individual part that makes up the collection of Parts? Commented Mar 25, 2014 at 21:58
  • Two properties: ProductName and Parts (an array of strings). Is there any way to use pipe to call my function to output information I need? Commented Mar 25, 2014 at 22:05
  • @oɔɯǝɹ Thanks and by the way your link doesn't work Commented Mar 25, 2014 at 22:14
  • I believe the expression in a calculate property needs to be wrapped in braces {} , not parens (). Commented Mar 25, 2014 at 22:17
  • For adding a property you'll need to use Add-Member, see: technet.microsoft.com/en-us/library/ff730946.aspx Commented Mar 25, 2014 at 22:23

2 Answers 2

3

The expression in your calculated property must be in curly brackets, not parentheses. Change this:

Select-Object -Property ProductName, `
    @{Expression=(GetParts $_.ProductName); Label="Parts"}

into this:

select ProductName, @{Label='Parts';Expression={GetParts $_.ProductName}}
Sign up to request clarification or add additional context in comments.

Comments

2

To get it to accept values from the pipe use the parameter option ValueFromPipeline like this:

function GetParts{
    Param(
        [parameter(ValueFromPipeline = $true)]
        [string]$ProductName
    )
    Process{
       $parts = @()
       ....
       return New-Object PSObject -Property @{ProductName=$ProductName;Parts=$parts}
    }
}

That will return an object with the desired properties, and accept values from the pipe.

2 Comments

Why do you use New-Object when you can just use Add-Member to add a new property to an existing object?
@oɔɯǝɹ Because he is inputting a string, so if I just added a property it wouldn't output an object with two properties it would output a string containing the input, and a property on that string object with the array of parts. In short, it didn't give him the output he desired.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.