Is This a Case to Use Invoke-Expression as the Easiest Route?

I have created a module that has several similar functions (e.g. Get-Data01, Get-Data02, etc)

Each of these similar functions always takes the same 2 input parameters; however, the processing inside each function varies greatly

My first approach was to use a formatted string like …

"Get-Data{0:D2} -1 {1} -2 {2}" -f $DrivingNum, $Param1, $Param2

… to then pass into an Invoke-Expression call

But I found an article direct from Microsoft saying to avoid using Invoke-Expression as much as possible

Can you think of any other slick / clever way to drive which of my Get-Data* functions gets called?

1 option i thought of (which would involve more lines of code) was something like

$params = @{ 1 = $1; 2 = $2 }

switch ( $DrivingNum ) {
1 { Get-Data01 @Params }
2 { Get-Data02 @Params }
etc

Or is this a case when Invoke-Expression is the easiest route?

I tend to default to writing code in the way that most people could read the code and easily understand. Following that guideline, I would use switch. The formatted string method takes more interpretation to comprehend.

Jackson,
Welcome to the forum. :wave:t3:

I actually wonder why you went with different functions. If they all take the same two parameters and you only switch between them depending on one external criteria you could use one big function (Get-Data) and add a third parameter to decide what logic to apply internally. :man_shrugging:t3:

gotcha!

so avoid the question of possible security to let the “cleaniless” of the code appearance be the motivating factor

thanks :grin:

i dove head-first before doing better / deeper planning

also … i try to avoid “long” source code files (i have 1 at work that’s grown to a lil over 1k lines & i always dread cracking open that novel)

plus the way i was organizing the Repo it made logical sense to keep the Get-Data01 file “close to” it’s fellow *01 resources

for more context: i started this month strong working thru the Advent Of Code puzzles & storing my solutions in a GitHub repo

so the actual “similar” functions were Measure-Answer*

and after day 04 i wanted to be able to consistently call Measure-Answer 4 vs Measure-Answer04

some additional motivation was that if i ever really cleaned up the module i could trim down the Exported Function list to JUST the Measure-Answer command vs Measure-Answer01 … Measure-Answer25

Yeah, I actually don’t use invoke-expression myself unless there’s no other way, and I can usually find another way.

And my earlier comments were really about the overall code appearance rather than the security implications of the two options. I worked for years with a team whose understanding of PowerShell was elementary to non-existent, so clarity was always a high priority if anyone was ever going to understand it.

The good thing is that the switch example you gave can avoid invoke-expression and be easier to read. So it’s a win-win on those two points.

<offtopic>

I ran into a script a while back that was 27K lines !!! Now that is a Novel.

</offtopic>

well now i feel better about my 1k-er :rofl: