Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell如何处理可传递/嵌套模块导入?_Powershell_Module - Fatal编程技术网

powershell如何处理可传递/嵌套模块导入?

powershell如何处理可传递/嵌套模块导入?,powershell,module,Powershell,Module,我一直认为在我当前的powershell控制台中执行导入模块,将使该模块在整个控制台中可用 然而,最近我了解到,当从模块中导入模块时,事情变得越来越复杂 比较: 模块会话状态 只要将模块或其嵌套模块之一导入会话,就会创建模块会话状态。当模块导出cmdlet、函数或脚本等元素时,对该元素的引用将添加到会话的全局会话状态。但是,当元素运行时,它将在模块的会话状态中执行 我所能观察到的是: # This is mod1.psm1 Import-Module -Force mod2.psm1 # en

我一直认为在我当前的powershell控制台中执行
导入模块
,将使该模块在整个控制台中可用

然而,最近我了解到,当从模块中导入模块时,事情变得越来越复杂

比较:

模块会话状态 只要将模块或其嵌套模块之一导入会话,就会创建模块会话状态。当模块导出cmdlet、函数或脚本等元素时,对该元素的引用将添加到会话的全局会话状态。但是,当元素运行时,它将在模块的会话状态中执行

我所能观察到的是:

# This is mod1.psm1
Import-Module -Force mod2.psm1
# end mod1.psm1
-

。。。当使用
Get Module

PS> Import-Module -Force mod2.psm1
。。。这将通过mod2提供mod2的功能

我甚至不完全理解这里的行为,但现在有趣的是:

# This is mod1.psm1
function fn_load_in_mod1 {
  Import-Module -Force mod2.psm1

  # mod2 stuff can be used here
}
# end mod1.psm1

PS> Import-Module -Force mod1.psm1
PS> # But here, no mod2 stuff is available anymore!
我了解到,要使我的控制台能够使用mod2,我需要:

# This is mod1.psm1
function fn_load_in_mod1 {
  Import-Module -Force mod2.psm1 -Global

  # mod2 stuff can be used here, and with the -Global switch also "outside"
}
# end mod1.psm1
但我无法理解会话状态的内容


我应该如何“正确”地从模块中导入模块?

据我所知,导入后我们需要先调用模块1,然后才能全局访问模块2。如果我们直接调用module2函数而不调用module1,那么就我所见,我们需要在导入之后首先调用module1,然后才能全局访问module2。如果我们直接调用module2函数而不调用module1,则会抛出错误
# This is mod1.psm1
function fn_load_in_mod1 {
  Import-Module -Force mod2.psm1 -Global

  # mod2 stuff can be used here, and with the -Global switch also "outside"
}
# end mod1.psm1