如何在使用 ValueFromPipelineByPropertyName 时获取原始管道对象



我正在用C#构建一个Cmdlet。

当使用ValueFromPipelineByPropertyName=true绑定参数时,我经常想将属性绑定的原始管道对象传递回管道。如何获取对此原始对象的引用?

因此,如果这是我的 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);
    }
}

我希望能够在我的脚本中做到这一点

# $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

你只需要包含另一个具有 ValueFromPipeline=$true 属性的参数。

最新更新