Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a wrapper powershell command that forwards all arguments to a delegate command?

wrapper is supposed to just call delegate, forwarding the arguments that were passed to wrapper.

Given

.\wrapper.ps1 1 2

When delegate.ps1 contents:

param($one, $two)
write-host "one=$one and two=$two"

Then expect output to be:

one=1 and two=2

wrapper.ps1 contents:

.\delegate # How do I forward all args passed to this wrapper script to the delegate script?

If I try the following implementation of wrapper.ps1:

.\delegate $args

The output is incorrectly:

one=1 2 and two=
like image 873
successhawk Avatar asked Sep 15 '25 13:09

successhawk


2 Answers

What you are looking for is Splatting

Use the @ symbol

.\delegate @args
like image 94
Alex Moffitt Avatar answered Sep 18 '25 16:09

Alex Moffitt


PowerShell provides a ProxyCommand class designed to make this easy. To generate the text of a wrapper function for a command, say MyCommand, you would do

$wrapper = [System.Management.Automation.ProxyCommand]::Create((get-command MyCommand))
like image 39
Bruce Payette Avatar answered Sep 18 '25 14:09

Bruce Payette