Author Posts

June 4, 2014 at 7:21 am

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)

June 4, 2014 at 7:28 am

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.

June 4, 2014 at 9:36 am

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.

June 5, 2014 at 7:40 am

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.