0

Background

In this question I describe how I have a custom PSModule setup and include various other .ps1 files using the module manifest NestedModules node.

The problem I describe in this question is how I can call any function in the NestedModule files, from the main PSModule script files, but one NestedModule script can NOT call functions in the other NestedModule script file.

I believe I got around this problem by exporting the functions I wanted to call in the NestedModules. Then it was possible to call the (exported) function from one NestedModule, where that function was defined in another NestedModule.

Variable vs Functions

Now, using the exact same setup (NestedModules), I include a script file that defines a number of constant variables, so that I can use these throughout my scripts, rather than having magic strings littered throughout.

I found that the constant variables worked, without any need to export them. I can use them in the main PSModule script files, or in other NestedModule script files without any issue or need to export or treat specially in any way!

This is great. But I'd love it if somebody can give a definitive explanation why this works? Is it known that variables are treated more simply within PSModules / NestedModules? It did occur to me that maybe even a PowerShell version increment has allowed this to work - it's been a long time since I asked the original question - and maybe even the Functions would work without the explicit exports in the current version?

Any comments or conclusive explanations much appreciated.

5
  • How are you defining / assigning values to the variables? $global:myVar would behave differently to $script:myVar for example. Also, it would be a stylistic preference for me (rather than any sort of best-practise), but to avoid polluting the global namespace with variables that might clash with other modules, you could consider a function that returns either the individual values (Get-MyVar) or returns a pacustomobject with all your values as properties (Get-ManofestConstants or whatever). Commented Jun 4, 2024 at 15:38
  • They're not global vars, just script level. But it appears that the script level vars are available to all NestedModules. Which is exactly what I want, but just note the difference between variables and functions. Commented Jun 4, 2024 at 16:30
  • You might want to read the help article called About_Scopes. It's available online here. Commented Jun 5, 2024 at 9:46
  • My question is specifically around why variables are accessible without explicit export, whereas functions are not. For example, I asked ChatGPT and it gives this answer: When you use the NestedModules property in a PowerShell module manifest, each script or module listed in NestedModules is essentially treated as an independent module. This means that variables defined in one nested module are not automatically accessible to other nested modules. Commented Jun 5, 2024 at 10:32
  • ChatGPT answer would be inline with functions (the link in my initial question). BUT ChatGPTs answer is incorrect, or at least out-of-date. I'm not finding this to be the case at all. All variables defined in the NestedModule ps1 are available to all other NestedModules without explicit export. Commented Jun 5, 2024 at 10:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.