PowerShell 模块,从另一个 NestedModule 调用 NestedModule 中的函数 [英] PowerShell module, call function in NestedModule from another NestedModule

查看:61
本文介绍了PowerShell 模块,从另一个 NestedModule 调用 NestedModule 中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Powershell 模块,并且在清单中我声明了主模块和两个嵌套模块.模块结构如下:

I have a Powershell module and in the manifest I have declared the primary module and two nested modules. The structure of the module is as follows:

- [dir] Pivot.DockerAdmin
    - [manifest] Pivot.DockerAdmin.psd1
    - [main module file] Pivot.DockerAdmin.psm1
    - [nested script] DockerfileScripts.ps1
    - [nested script] DockerCliScripts.ps1

什么有效主模块 (Pivot.DockerAdmin.psm1) 可以毫无问题地调用嵌套模块文件(DockerfileScripts.ps1、DockerCliScripts.ps1)中的函数.请注意,除了清单文件中的条目外,没有包含这些文件的特定逻辑.

What works The Primary Module (Pivot.DockerAdmin.psm1) can call functions in the Nested Module files (both DockerfileScripts.ps1, DockerCliScripts.ps1), without a problem. Note, there's no specific logic to include these files, other than the entry in the manifest file.

什么不起作用一个嵌套模块脚本文件 (DockerfileScripts.ps1) 无法调用另一个嵌套模块脚本文件 (DockerCliScripts.ps1) 中的函数.

What does NOT work One Nested Module script file (DockerfileScripts.ps1) cannot call functions in the other Nested Module script file (DockerCliScripts.ps1).

嵌套模块只是简单的脚本文件.因此,实际上,我正在使用 NestedModule 概念对其他文件中的某些函数进行逻辑分组.

The nested modules are just simple script files. So in effect, I'm using the NestedModule concept to logically group some functions in other files.

模块设置正确.我对此很有信心,因为我什至在没有任何特殊处理的情况下在构建盒上运行了 Pester 测试.

The module is setup correctly. I'm confident about this, because I even have Pester tests running on a build box without any special treatment.

我希望能够从另一个嵌套模块调用嵌套模块中的函数,就像主模块可以调用任何嵌套模块中的函数一样,但是这失败了 无法识别的命令错误.

I expect to be able to call a function in a nested module from another nested module, in the same way the primary module can call functions in any nested module, but this fails with an unrecognised command error.

如果这是不可能的,是否有关于在 PS 模块中组织脚本文件的建议,以便类似的脚本划分/关注点分离是可能的?

If this is not possible are there any recommendations around organising script files within PS modules, so that a similar division of scripts / separation of concerns is possible?

推荐答案

所以如果你看看我在这里发布的例子:

So if you look at the example I posted here:

https://stackoverflow.com/a/55064995/7710456

我会稍微扩展一下.

我再次查看并为所有模块创建了一个模块清单,所有这些模块都需要遵循 PowerShell 模块的标准(在与 PowerShell 模块同名的文件夹中,位于存在于 PSModulePath 中)

I took another look at it and created a module manifest for all of the modules, and all of those modules need to follow standards for PowerShell modules (in a folder with the same name as the PowerShell module, in a location that is present in the PSModulePath)

Write-BazFunctions.psm1:

Write-BazFunctions.psm1:

Function Write-Baz {
    return "Baz"
}

Write-BarFunctions.psm1:

Write-BarFunctions.psm1:

Function Write-Bar {
    return "Bar"
}

Function Write-BarBaz {
    $bar = Write-Bar;
    $baz = Write-Baz;
    return ("{0}{1}" -f $bar, $baz)
}

Write-FooFunctions.psm1

Write-FooFunctions.psm1

Function Write-Foo {
    return "Foo"
}

Function Write-FooBar {
    $foo = Write-Foo
    $bar = Write-Bar
    return ("{0}{1}" -f $foo, $bar)
}

Function Write-FooBarBaz {
    $foobar = Write-FooBar
    $baz = Write-Baz
    return ("{0}{1}" -f $foobar, $baz)
}

Function Write-FooBazBar {
    $foo = Write-Foo
    $bar = Write-Bar
    $baz = Write-Baz
    return ("{0}{1}{2}" -f $foo, $bar, $baz)
}

现在 - 差异.在 Write-BarFunctions 的清单中(注意是必需的,不是嵌套的):

Now - differences. In the manifest for Write-BarFunctions (note required, not nested):

RequiredModules = @('Write-BazFunctions')

注意与上面链接的原始答案的另一个不同之处,我直接针对 psm1 文件,而是仅通过模块名称引用它们.

note another difference from my original answer linked above I was targeting the psm1 files directly, instead, reference them just by the module name.

一旦我这样做了,我就能够导入 Write-FooFunctions 并且所有函数都可用了.由于 Write-BarFunctions 中的 Write-BarBaz 调用了 Write-BazFunctions 中的 Write-Baz,您可以看到这将允许嵌套模块相互引用.

Once I did this, I was able to import Write-FooFunctions and all of the functions came available. Since Write-BarBaz in Write-BarFunctions calls Write-Baz from Write-BazFunctions you can see that this will allow for nested modules to reference one another.

这篇关于PowerShell 模块,从另一个 NestedModule 调用 NestedModule 中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆