Function pass-through arguments

Hello,

We have a powershell file ‘function.ps1’ with many custom functions. And many scripts use these functions from this file. I want to rewrite/improve and rename all functions. But I don’t want to change all individual scripts at once. I want the old function to redirect it to the new function. Some functions have 1 argument and some 5 arguments and I want a simple uniform way to redirect the arguments to the new function.

Example:
This is what we have.

function writelog($file,$text) {
   old-code
}
function custom-writelog($file,$text) {
   new-code
}

This is what I want (but this doesn ́t work)

function writelog() {
   custom-writelog $args
}

function custom-writelog($file,$text) {
   new-code
}

I now use this, but is this the best way?

function writelog() {
	switch ($args.count) {
		1 { custom-writelog $args[0] }
		2 { custom-writelog $args[0] $args[1] }
		3 { custom-writelog $args[0] $args[1] $args[2] }
		4 { custom-writelog $args[0] $args[1] $args[2] $args[3] }
		5 { custom-writelog $args[0] $args[1] $args[2] $args[3] $args[4] }
		6 { custom-writelog $args[0] $args[1] $args[2] $args[3] $args[4] $args[5] }
		}
}

Bert

Hi Bert,

Welcome to the forums! In order to ensure that people can determine what is code and what is not, please go back and format your code using the </> button. This will make your code readable and easier to digest. Please also show any error messages you are getting along with putting all the code into the post so we can see what you are trying to do. I am including a link that will help you in getting your code formatted on the forums.

Guide for formatting code

I think the more robust, reliable and most professional way wqould be not to redirect them, but to re-write them completely in a module. There you could set up aliasses for the old function names and parameter names. This way you can keep your old scripts but use the new names for newer scripts.

1 Like

Hello Olaf,
That is an option, but now we have many different machine and now we have to update one file.
Bert

Is that a question? :wink: