模块自动加载是否可靠?

alx*_*x9r 7 powershell module autoloader

环境

我有以下文件夹结构,我保留PowerShell模块:

C:
  PsModules
     ...
     util
        util.psm1 (this contains implementation of 'Test-Function')
        util.test.ps1
     ftp
        ftp.psm1
        http.test.ps1
     ... 
Run Code Online (Sandbox Code Playgroud)

大约有50个文件夹和模块c:\PsModules.

我已将环境变量设置PSModulePath为包含c:\PsModules.这似乎符合Microsoft文档此答案中描述的 "格式良好的模块"的条件.

症状

Test-Function从ISE调用时有时无法自动找到.实际上,在任何新发布的ISE上,总会有一些(似乎不可预测的)模块无法自动找到.Test-Function例如,无法自动查找,如下所示:

PS C:\> Test-Function
Test-Function : The term 'Test-Function' is not recognized as the name 
of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is 
correct and try again.
...
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

乍一看,这似乎表明这util.psm1不是"格式良好".如果它不是"格式良好",那么ListAvailable应该不起作用.但它确实有效:

c:\> get-module -ListAvailable util
Directory: c:\PsModules\util

ModuleType Version    Name                 ExportedCommands
---------- -------    ----                 ----------------
Script     0.0        util

PS C:\> Test-Function
...
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

此外,在调用Get-Command模块后,模块中的命令可供一般使用:

c:\> Get-Command -Module util
CommandType     Name                             ModuleName
-----------     ----                             ----------
Function        Test-Function                    util
c:\> Test-Function
Call to Test-Function succeeded!
Run Code Online (Sandbox Code Playgroud)

问题

  1. 自动发现和模块自动加载是否可靠且可预测?

  2. 我怎么解决为什么powershell有时在调用之前找不到命令Get-Command -module

  3. 依靠powershell自动加载模块是不好的做法吗?如果是这样,自动加载模块的好习惯是什么?

bri*_*ist 3

我不能说模块自动加载是否可靠,但我个人并不在完成的代码中依赖它。

如果我正在编写脚本或模块,我总是使用Import-Module TheModule -ErrorAction Stop, 并且经常使用#Requires -Module AciveDirectory,TheModule,SQLPs来确保模块可用。

对于 PowerShell 控制台或 ISE 中的交互式使用,我通常依赖自动加载,但如果失败,我只需Import-Module手动进行会话。

在我总是希望为交互式会话加载特定模块的情况下,我将其加载到配置文件中。要查看各种配置文件,请运行以下命令(从 ISE 和控制台):

$profile | Get-Member -MemberType NoteProperty
Run Code Online (Sandbox Code Playgroud)

您可以根据您希望模块可用的用户和主机来决定将导入模块的代码放置在何处。

到目前为止,我只为posh-git这样做,但它似乎很适合您的用例。