1

我正在用 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
4

1 回答 1

1

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

于 2018-01-08T17:37:08.333 回答