How to forward pipeline input from a binary proxy cmdlet? #21356
-
There's a somewhat well-known pattern for implementing a proxy PowerShell function: function Proxy {
[CmdletBinding(...)]
param(...) # either copy parameters manually ...
dynamicparam {
# ... or use dynamicparam
}
begin {
$sb = {Wrapped-Command ...}
$sp = $sb.GetSteppablePipeline()
$sp.Begin($PSCmdlet)
}
process {
$sp.Process($_)
}
end {
$sp.End()
}
} For various reasons, I would like to implement a similar proxy as a binary cmdlet. Most parts map 1:1 between PowerShell and C#, but I can't seem to figure out what to do in the [Cmdlet(...)]
class ProxyCommand : ... {
// copy parameters from wrapped cmdlet using dynamic parameters
public object GetDynamicParameters() {...}
private PowerShell _ps = PowerShell.Create();
private SteppablePipeline? _pipeline;
protected override void BeginProcessing() {
base.BeginProcessing();
_pipeline = _ps.GetSteppablePipeline();
_pipeline.Begin(this);
}
protected override void ProcessRecord() {
base.ProcessRecord();
_pipeline!.Process(); // what do I do here, since I don't know which of the forwarded dynamic params accept pipeline input?
}
protected override void EndProcessing() {
base.EndProcessing();
_pipeline!.End();
}
} A possible option would be to manually check which of the possible pipeline parameters was filled from the pipeline and forward it, but that requires quite a lot of needless coupling between the proxy and the wrapped cmdlet. From a cursory inspection of the pipeline implementation in the PowerShell engine, I don't see a simple way to poke at the input object without a lot of reflection. Is there a clean solution to this problem? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Do you need
And PowerrShell will populate that on each iteration of process record. |
Beta Was this translation helpful? Give feedback.
-
I guess one dubious approach would be to remove Still, I'm open to less hacky alternatives. :) |
Beta Was this translation helpful? Give feedback.
-
This uses legitimate methods..
The test cases pipe data in which are then passed to ConvertTo-JSON
|
Beta Was this translation helpful? Give feedback.
I don't think there is a supported way to access that info at the moment. A less error prone (but not supported) path might be to access the internal property
PSCmdlet.CurrentPipelineObject