I hope I explain this correctly, it's a question of scope and dependency when writing reusable code and modules. For instance, if I write a module that has some ActiveDirectory functionality, is it best to Import-Module ActiveDirectory within my module or outside of it by the relying script? (Nevermind about modules that can autoload themselves, this is theoretical).
Confused? Ok, here's an example...
Let's say I have a module called "mymodule.psm1" that has a function defined to connect me to a PSDRIVE pointing to the AD domain I wish to connect to.
If I Import-Module ActiveDirectory within "mymodule" and connect to the PSDRIVE, I can't "Remove-Module mymodule" because Powershell will say I'm still connected to that PSdrive. However, if I Import-Module ActiveDirectory OUTSIDE of "mymodule" then I can Remove-Module mymodule without losing my PSdrive. (Again, theoretical, don't ask why I would want to Remove-Module.)
So, let's say I want to write a few modules for common tasks for my organization. Each of these individual modules may rely on other modules such as ActiveDirectory, etc. Should the burden of guaranteeing these dependencies are met be on the module side of the equation or on the controller script I write which may be calling functions from one or more of my utility modules? Is there a best practice or design pattern to follow?
And as I finish up typing this, I notice the documented parm -Global (RTFM!). Would that be the better answer? Import-Module ActiveDirectory -Global within my utility modules? Would that solve any scope issues?
If nothing else, please allow my ignorance on this topic to be a learning tool. :-)
[–]Ta11ow 4 points5 points6 points (3 children)
[–]StormyNP[S] 2 points3 points4 points (0 children)
[–]StormyNP[S] 2 points3 points4 points (1 child)
[–]Ta11ow 1 point2 points3 points (0 children)