使用 ValueFromPipelineByPropertyName 时如何获取原始管道对象?
How can I get the original pipeline object when using 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 属性的参数。
我正在用 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 属性的参数。