locked
PowerShell module, call function in NestedModule from another NestedModule. RRS feed

  • Question

  • Hi,

    I have a Powershell module set up and in the manifest I have declared the primary module and two nested modules.

    Note: 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.

    What works

    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.

    Also, the Primary Module can call functions in the Nested Module files, without a problem.

    What does NOT work

    One Nested Module script file cannot call functions in the other Nested Module script file.

    Should this work? If it should, is there some special syntax I need to use? If it should not work, then is there any recommendation about breaking scripts up into lots of files and allowing each of them to call onto the others?

    I'm aware of "dot sourcing" but I'm pretty sure that using PowerShell modules is an advance on dot sourcing and so feel like there should be some way of achieving this?

    Thanks.

    Friday, March 15, 2019 10:02 AM

All replies

  • Hi,

    Try doing a get <g class="gr_ gr_24 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="24" id="24">childitem</g> the folder with <g class="gr_ gr_49 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="49" id="49">recurse</g>. For each of the files and nested modules files load.

    Get-ChildItem -Path PATH_TO_MODULE\*.ps1 -Recurse |
    
    ForEach-Object {
        . $_.FullName
    }
    Cheers!



    • Edited by CITLead Tuesday, March 19, 2019 9:34 PM remove css code
    Tuesday, March 19, 2019 9:32 PM
  • Hi,

    Try doing a get <g class="gr_ gr_24 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="24" id="24">childitem</g> the folder with <g class="gr_ gr_49 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" data-gr-id="49" id="49">recurse</g>. For each of the files and nested modules files load.

    Get-ChildItem -Path PATH_TO_MODULE\*.ps1 -Recurse |
    
    ForEach-Object {
        . $_.FullName
    }
    Cheers!



    You post is corrupted.  Turn off Grammarly for this site or upgrade to fixed version.


    \_(ツ)_/

    Tuesday, March 19, 2019 9:49 PM
  • Sorry for the delayed reply on this one.

    I'm not sure I follow your gist. When I say "multiple nested module files", I mean that I have declared these script files in the PowerShell Modules Manifest (psd1) file.

    E.g. I have this module structure:

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

    In the module manifest I've declared:

    NestedModules = @('DockerfileScripts.ps1','DockerCliScripts.ps1')

    Now to reiterate what works and what doesn't, but this time with specific reference to the filenames in the structure above.

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

    Thanks.

    Monday, March 25, 2019 10:28 AM