环境
我有以下文件夹结构,用于保存 powershell 模块:
C:
PsModules
...
util
util.psm1 (this contains implementation of 'Test-Function')
util.test.ps1
ftp
ftp.psm1
http.test.ps1
...
大约有50个文件夹和模块c:\PsModules
.
我已经设置了环境变量PSModulePath
包括c:\PsModules
。这似乎满足“格式良好的模块”的条件微软文档中有描述 https://msdn.microsoft.com/en-us/library/dd878350%28v=vs.85%29.aspx and 这个答案 https://stackoverflow.com/a/23168236/1404637.
Symptoms
有时 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
乍一看,这似乎表明util.psm1
不是“格式良好”。如果它不是“格式良好”,那么可用列表不应该工作。但它确实有效:
c:\> get-module -ListAvailable util
Directory: c:\PsModules\util
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 0.0 util
PS C:\> Test-Function
...
+ FullyQualifiedErrorId : CommandNotFoundException
此外,调用后Get-Command
对于模块来说,模块中的命令是可以通用的:
c:\> Get-Command -Module util
CommandType Name ModuleName
----------- ---- ----------
Function Test-Function util
c:\> Test-Function
Call to Test-Function succeeded!
问题
自动发现和模块自动加载应该是可靠且可预测的吗?
如何排查为什么 powershell 有时在调用之前找不到命令Get-Command -module
?
依赖 powershell 自动加载模块是不好的做法吗?如果是这样,自动加载模块的良好做法是什么?