Is module auto-loading meant to be reliable?

a 夏天 提交于 2019-12-05 05:31:30

I can't speak to whether module auto-loading is intended to be reliable, but I personally do not rely on it in finished code.

If I'm writing a script or module, I always use Import-Module TheModule -ErrorAction Stop, and often use #Requires -Module AciveDirectory,TheModule,SQLPs to ensure that the modules are available.

For interactive use in the PowerShell console or ISE, I do generally rely on auto-loading, but if it fails I just Import-Module manually for the session.

In situations where I always want a specific module loaded for an interactive session, I load it in a profile. To see the various profiles run this (from both ISE and Console):

$profile | Get-Member -MemberType NoteProperty

You can decide where you want to place the code for importing the module based on which user(s) and in which host(s) you want the module to be available.

So far I only do this for posh-git, but it seems like it would fit your use case well.

In case this is helpful to anyone else, I think this may be a problem exclusive to ISE. I could not repro but recently went around with MS on inconsistent ISE workflow behavior and after some effort the issue was closed without a solution, with the official answer being to not use ISE which is not approved for production and instead use the native shell. It was a realistic answer for us, never saw the issues in native shell. I wonder if it's the same on this symptom?

Omzig

I have found that the FunctionsToExport section in the module manifest cannot be set to *

THIS IS BAD:

# Functions to export from this module, for best performance, do not use 
# wildcards and do not delete the entry, use an empty array if there are no 
# functions to export.
FunctionsToExport = '*'

THIS IS GOOD:

# Functions to export from this module...
FunctionsToExport = 'Test-Function, Test-Function2'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!