Can I have include files in modules?

This topic contains 3 replies, has 4 voices, and was last updated by  Jaykul 3 years, 4 months ago.

  • Author
    Posts
  • #15891

    GS
    Participant

    Hello,

    We have module which is distributed to client machines via GPO. We would like to have several parts of company to contribute cmdlets inside that module but don't want them to interfere with each other while they are editing their modules. Something like #include statement in ASP where you specify what you need to merge into pipeline.
    So ultimately my dream is to have module called say Company.psm and inside that module will bunch of #include statements to other PSM files or TXT files which will be interpreted as powershell functions.
    How this can be done (not specific scenario I outlines but in general where only one PSM file exists and yet cmdlets are pulled from other TXT,PSM files into it)

  • #15892

    Dave Wyatt
    Moderator

    Absolutely. Script modules can have other Import-Module statements, or you can dot-source PS1 files. Either one works to make the content of those files become available when your "parent" module is imported. Module manifests also have a NestedModules and ScriptsToProcess array which can be used to accomplish the same thing, but in a more static way.

  • #15895

    Martin Nielsen
    Participant

    If your module is correctly installed in the clients $env:PSModulePath folder(s), you can use the following to ensure your module is loaded before script execution:


    #Requires -Modules CompanyModule

    If the module is not present in the $env:PSModulePath folder(s), PowerShell throws a terminating error before any of your script is executed.

    More about #Requires at http://technet.microsoft.com/en-us/library/hh847765.aspx

    But as I re-read your post, it seems more like what you're looking for is a version control system, akin to what GitHub provides. You have a Master branch that is pushed to clients, but your developers each work on their own branch of the project, and then commit their changes back to the Master (or test branch first) when they've added more code.

  • #15920

    Jaykul
    Participant

    Dot-sourcing is what you want. It just means putting the "." invocation call before the script path, and it results in the content of the script being executed as though it were typed in on that line.

    #MainModule.psm1
    . $PSScriptRoot\SomeFunctionsOne.ps1
    . $PSScriptRoot\SomeFunctionsTwo.ps1
    . $PSScriptRoot\SomeFunctionsThree.ps1

    If you're building bigger reusable pieces, use
    Import-Module AnotherModule -ErrorAction Stop
    because neither #requires nor the RequiredModules attribute in the module manifest will guarantee that the commands are available without a prefix.

You must be logged in to reply to this topic.