Shared Private Functions In a Module

Tagged: 

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Stephen Yeadon Stephen Yeadon 1 year, 11 months ago.

  • Author
    Posts
  • #21351
    Profile photo of Stephen Yeadon
    Stephen Yeadon
    Participant

    I have created a powershell module. lets call it Helper. Helper contains:
    Helper.psd1
    X.psm1
    Y.psm1
    Private.psm1

    Private.psm1 contains functions that are used by X and Y but should not be publicly visible. X and Y consume the private.psm1 functions internally and export their own functions that wrap the low level private functions. The Helper.psd1 file further reiterates the functions that are exported in X and Y. However this does not seem to be working. Am I going about this right or do I need to duplicate the private functions across X and Y. (unhappy smiley)

  • #21353
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    We'd need to see your psd1 and psm1 files to know what's going on for sure. I've fiddled with some dummy files, trying several sets of options on the psm1 and psd1 files, and so far haven't been able to create a situation where unwanted functions wound up being exported.

  • #21354
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    On a side note, I rarely bother with nested modules. If there is code that's shared across multiple files inside a script module, I tend to just create PS1 files and dot-source it. A single script module boundary tends to be enough, most of the time.

  • #21355
    Profile photo of Stephen Yeadon
    Stephen Yeadon
    Participant

    Sorry. That not it at all.

    I can't see the private function from module X or Y. I'm not even not sure if I should be able to. I'll try to clarify the project structure.

    Helper.psd1
    – Private.psm1
    — Get-Stuff()
    – X.psm1
    — Do-Stuff()
    – Y.psm1
    — Do-OtherStuff()

    Do-Stuff() internally calls Get-Stuff(), so i need to be able to see this function from within a different module file. The same is true for Do-OtherStuff(). However I do not want to expose Get-Stuff() outside of the Helper module. This should only expose Do-Stuff and Do-OtherStuff().

    Hope that is clearer.

  • #21356
    Profile photo of Stephen Yeadon
    Stephen Yeadon
    Participant

    Thanks. Dot sourcing a ps1 file is a obvious solution when you think about it.. It worked perfectly.

You must be logged in to reply to this topic.