Chad Schouggins
Chad Schouggins

Reputation: 3521

How can I get the original pipeline object when using ValueFromPipelineByPropertyName?

I'm building a Cmdlet in C#.

When binding parameters using ValueFromPipelineByPropertyName=true, I often would like to pass the original pipeline object from which the properties were bound back into the pipeline. How can I get a reference to this original object?

So if this is my cmdlet

[Cmdlet(VerbsLifecycle.Start, "Foo")]
public class StartFooCommand : PSCmdlet
{
    [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
    public String Name { get; set; }

    protected override void ProcessRecord()
    {
        // Perform various Foo-related activities...

        Object pipelineObject = GetTheObjectPassedInFromThePipeline();

        WriteObject(pipelineObject);
    }
}

I would like to be able to this in my script

# $foo would be some return value from another cmdlet
$foo = New-Object PSObject -prop @{ Name = "Frank"; Bar = "Baz" }
$foo | Get-Foo | Use-Foo
#    ^         ^ Here I want to pass the original $foo object to the next cmdlet
#    L Name gets bound from my object to my property

Upvotes: 1

Views: 304

Answers (1)

Mike Shepard
Mike Shepard

Reputation: 18166

You just need to include another parameter which has the ValueFromPipeline=$true attribute.

Upvotes: 1

Related Questions