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

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.

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

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.

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.

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?

1 Answers

0
Ryan On Best Solutions

So if you look at the example I posted here:

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

I'll expand on it a bit.

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:

Function Write-Baz {
    return "Baz"
}

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

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)
}

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

RequiredModules = @('Write-BazFunctions')

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

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.