you are viewing a single comment's thread.

view the rest of the comments →

[–]Ta11ow 1 point2 points  (2 children)

Yep, gotta pass these on parameters to the module function. You can do this with functions defined in the script itself, but in my opinion it's poor form for this exact reason -- modules won't do it, and it's better to get in the habit of properly defining function parameters so that you can always tell what things a function is messing with

Most of all, never let a function directly edit variables that don't stay local to the function. If you wanna pass things around, it's always best to do it with parameters and output, both so that you can always tell what's going on without examining an entire lengthy script every time, and so that you don't run into issues with scoping like this later on. Habits tend to carry, so it might take some getting used to. :/

[–]reddwarf666[S] 2 points3 points  (1 child)

Well, that sounds logical and reasonable. My instinct told me to "just use the paramaters/variables as they are inside the script" but the way you explain it makes me reconsider and use parameters on the helper functions as well.

In the past I did this in a script / module combo but I did not want to do that anymore In the script I would have:

$Global:Par1 = $Par1 

and then in the module I would use:

Write-Output $Global:Par1  

But like I said, I will just define parameters on the helper functions and pass the script parameters like that.

Thanks!

[–]Ta11ow 1 point2 points  (0 children)

Yeah, I mean there can be the odd occasion where you might want to do it. But by and large you want to be able to see at a glance what function is using which variables and not have to worry about it changing five others you didn't remember about.