12

I am working on a PowerShell v3 module that needs to work with Types which are contained in a few external .NET assemblies.

I would like this module to be reasonably self-contained for ease of deployment and I do not want to rely on these assemblies being loaded in the GAC. Ideally, I would like to place the required assembly dll's in the module folder and then rely on PowerShell to automagically load these assemblies when the module is loaded.

I know that I could use the Add-Type command to brute force load the assemblies like so:

Add-Type -AssemblyName "Some.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=sometoken"

But I have alse read about the required assemblies property in a module manifest and I am hopeful that this approach could eliminate the seemingly fragile Add-Type code:

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

What is the most reliable way to reference the external assemblies within the module? Would declaring the dependency in the manifest implicitly load the assemblies when the module is loaded? If I took advantage of the module manifest to list the required assemblies would I still have to write code that loads the assemblies?

I'm really not looking for a simple "get it to work" solution as I already got this to work using the Add-Type approach... I am looking for guidance and reccomendations for the most reliable way of doing this.

syneptody
  • 1,250
  • 1
  • 10
  • 27

1 Answers1

11

The New-ModuleManifest documentation for its -RequiredAssemblies parameter agrees:

Specifies the assembly (.dll) files that the module requires. Enter the assembly file names. Windows PowerShell loads the specified assemblies before updating types or formats, importing nested modules, or importing the module file that is specified in the value of the RootModule key.

Use this parameter to list all the assemblies that the module requires, including assemblies that must be loaded to update any formatting or type files that are listed in the FormatsToProcess or TypesToProcess keys

And I cannot find anything different (eg. on MSDN).

Community
  • 1
  • 1
Richard
  • 106,783
  • 21
  • 203
  • 265
  • Do you know how to reliably list the assemblies in this property? If I used fully qualified names and placed the assembly dll in the module's folder is that sufficient or would I need to provide some sort of path? Where does the assembly loader look for these assemblies? – syneptody Sep 03 '13 at 21:23
  • @syneptody The loading rules are documented on [MSDN](http://msdn.microsoft.com/en-us/library/yx7xezcf%28v=vs.100%29.aspx): I don't think PSH does anything very special here, other than adding the module's directory to that search path. Perhaps following an example (like [PSCX](http://pscx.codeplex.com/))? – Richard Sep 03 '13 at 22:37
  • 1
    @syneptody From my usage I have the assembly name in the psd1 file, the assembly in the same directory of the script and it works without issue. For example `RequiredAssemblies = @('IHE.CDA.Generation.Trifolia')` – Tedford Oct 12 '15 at 19:05
  • Placing the dll files in the same directory saves headaches. Organizationally, might be worth putting them in a subdir thereof though? like ./bin ?? Any downside? – Allen Jan 12 '21 at 14:35