Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage dependencies between scripts in multi-file Powershell Module?

I have a large amount of powershell code that I've written over the course of a long project; these scripts perform a wide variety of functions, and most of them depend in some way on others within the scope of the project. Right now, the work is made up of a couple of files containing many functions each. Originally, in order to work with these scripts all the script files were sort of haphazardly dot-sourced into the environment.

However, I've learned recently that Powershell 2.0 introduces modules, and I would like to deploy these scripts together that way. Since a module's contents are all loaded together, I would like to split apart my files so that each script has its own file, in order to aid source control. However, I'm a little unclear about the connections between the scripts now.

I've done some testing and it seems that it's ok to move the Export-ModuleMember command for each function to the individual .ps1 files; this seems more like functions declaring their own scope like public and private scoping in C#. However, after doing that my .psm1 file contains nothing but this:

Get-ChildItem -recurse $psScriptRoot | where { $_.Extension -eq ".ps1" } | foreach { . $_.FullName }

Does that seem right? All the scripts are being dot sourced, and all the scripts refer to each other under that assumption. Should they instead refer to each other using their locations relative to $psScriptRoot?

Is there a way different than both these ways? Can anyone offer advice? I don't know much about these yet.

like image 394
bwerks Avatar asked Jun 02 '11 23:06

bwerks


2 Answers

I've seen a similar technique where each .ps1 file contains one function and the functions are sourced in the PSM1 file used in the WPK module and PSRemoteRegistry modules.

This line is the PSRemoteRegistry module:

Get-ChildItem -Path $PSScriptRoot\*.ps1 | Foreach-Object{ . $_.FullName }

I would say I like the technique over having one giant script files of functions.

like image 197
Chad Miller Avatar answered Sep 29 '22 11:09

Chad Miller


you could also look at creating a manifest (I don't actually know if you need a psm1 with a psd1).

Here was my use of a manifest:

New-ModuleManifest `
    -Path Fiddler.psd1 `
    -Author "Niklas Goude" `
    -CompanyName "http://www.powershell.nu/" `
    -ModuleVersion 1.0 `
    -Description "Module from http://www.powershell.nu/2011/03/14/fiddler/</a> - psd1 created by Matt @ amonskeysden.tumblr.com" `
    -FormatsToProcess @() `
    -RequiredAssemblies @("Fiddler.dll") `
    -NestedModules @() `
    -Copyright "" `
    -ModuleToProcess "Fiddler.psm1" `
    -TypesToProcess @() `
    -FileList @("Fiddler.psm1","Fiddler.dll")

I think the answer to your question would be to include your file list in the FileList parameter there.

I wrote some of my findings (including links to MS resources) here:

http://amonkeysden.tumblr.com/post/5127684898/powershell-and-fiddler

like image 43
Matt Avatar answered Sep 29 '22 11:09

Matt