【问题标题】:Powershell module nestingPowershell 模块嵌套
【发布时间】:2018-01-23 14:34:23
【问题描述】:

我想嵌套多个模块/脚本,我想将实际文件保存在多个目录中:

BuildTests[dir]  
|BuildTests.psd1  
|GenericTests[dir]  
||GenericTests.psd1  
||GenericTest1.ps1  
||GenericTest2.ps1  
|SpecificTests[dir]  
||SpecificTests.psd1  
||SpecificTest1.ps1  
||SpecificTest2.ps1  

我想为每个 .ps1 文件保留 1 个函数。
我尝试了以下方法:

在 BuildTests.psd1 中定义NestedModules = @('.\GenericTests\GenericTests.psd1','.\SpecificTests\SpecificTests.psd1')

在 GenericTests.psd1 中定义 ScriptsToProcess = @('GenericTest1.ps1','GenericTest2.ps1')
相应地配置SpecificTests.psd1

基本上这是可行的,但是在检查 get-module 时,我看到两次导入的 .ps1 文件中的所有功能,使用 -verbose 参数进一步检查 import-module,我可以看到一次是“正在加载模块...... test1.ps1”,下一行是“Dot-Sourcing the script file ....test1.ps1”。

我在 .psd1 中尝试了不同的变体(从 .ps1 更改为 .psm1 并将它们包含在嵌套模块中),但我似乎总是以导入重复或根本没有导入任何函数而告终。

【问题讨论】:

  • “每个 .ps1 文件 1 个函数”的任何原因?
  • 为了方便,也可以是 .psm1 文件

标签: powershell nested


【解决方案1】:

看起来它工作得很好,让我感到困惑的是冗长的输出列表多次导出/导入相同的函数:

PS D:\DevRepository\ParentModule> Import-Module .\ParentModule.psd1 -Verbose
详细:从路径“D:\DevRepository\ParentModule\ParentModule.psd1”加载模块。
详细:从路径“D:\DevRepository\ParentModule\Modules1\Modules1.psd1”加载模块。
详细:从路径“D:\DevRepository\ParentModule\Modules1\TestCase1.ps1”加载模块。
详细:点源脚本文件“D:\DevRepository\ParentModule\Modules1\TestCase1.ps1”。
详细:导出函数“test-case1”。
详细:导入函数“test-case1”。
详细:导出函数“test-case1”。
详细:导入函数“test-case1”。

然后检查模块 ExportedFunctions 属性:

PS D:\DevRepository\ParentModule> 获取模块 ParentModule |选择导出函数

导出函数
-----------------
{[test-case1, test-case1]}

实际上列出了键/值对:

PS D:\DevRepository\ParentModule> (Get-Module ParentModule).ExportedFunctions

核心价值
--- -----
测试用例1 测试用例1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2010-12-04
    • 2011-10-30
    相关资源
    最近更新 更多