脚本 (.ps1) 导入 ModuleA,ModuleA 导入 ModuleB。从脚本中我可以看到 ModuleB 中的一个函数(使用 Get-Command),但它被列为属于 ModuleA。我还可以修改/删除该功能。当执行进入 ModuleA 时,来自 ModuleB 的函数被列为属于 ModuleB(通过 Get-Command)并恢复到原始状态。
这是设计使然吗?
例子:
调用-Greeting.ps1
Import-Module .\Import-First.psm1
Get-Command -Module "Import-First" # Write-Greetings and Write-Goodbye
Get-Command -Module "Import-Second" # no functions found
Get-Command -Name "Write-Goodbye" # Write-Goodbye (module = Import-First) ***is this by design?***
Remove-Item "function:\Write-Goodbye"
Get-Command -Module "Import-First" # Write-Greetings
Get-Command -Module "Import-Second" # no functions found
Get-Command -Name "Write-Goodbye" # error: function doesn't exist (expected)
Write-Greetings
导入优先.psm1
Import-Module .\Import-Second.psm1
function Write-Greetings
{
Write-Host "hello world!" # hello world!
Get-Command -Module "Import-First" # Write-Greetings
Get-Command -Module "Import-Second" # Write-GoodBye
Get-Command -Name "Write-GoodBye" # Write-GoodBye (module = Import-Second) ***module changed since execution scope changed***
Write-GoodBye
}
导入-Second.psm1
function Write-Goodbye
{
Write-Host "bye bye!"
}