Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reliably reference external .NET assemblies in a PowerShell v3 module?

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.

like image 711
syneptody Avatar asked Aug 28 '13 19:08

syneptody


1 Answers

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).

like image 169
Richard Avatar answered Sep 28 '22 03:09

Richard