4

I'm not sure why this is failing, but if I Add-Type from a function then for whatever reason my code fails when I need to use the *.dll api.

function Add-References
{
    Add-Type -Path "C:\myDllPath.dll"
    Add-Type -Path "C:\myDllPath2.dll"
}

I have tried making the function global, the dot operator... Keep in mind this function is contained in a module and is being called by a script. My goal is to just use the function to declare my references rather than copy paste these paths in all of my scripts.

TacoMaster6000
  • 304
  • 3
  • 12

1 Answers1

2

It would help if you included the error you're receving.

But I suggest an alternative: use the RequiredAssemblies key in your module manifest:

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. This includes assemblies that must be loaded to update any formatting or type files that are listed in the FormatsToProcess or TypesToProcess keys, even if those assemblies are also listed as binary modules in the NestedModules key.

briantist
  • 45,546
  • 6
  • 82
  • 127
  • This is great! Thanks. I have a quick question; I have a module MyModule.psm1, do I create a Manifest with the same naming convention MyModule.psd1 OR specify MyModule.psm1 is a nested module? It's not too clear on how to use the manifest but I'm assuming I create one manifest for multiple similar modules, nest the modules in the manifest and import the manifest in my scripts. – TacoMaster6000 Jul 07 '16 at 14:04
  • 1
    @user3812871 the manifest has the same name but with psd1 extension (`MyModule.psd1` corresponds to `MyModule.psm1`), at least typically. The manifest specified the `RootModule` so I suppose in theory they could be named differently but I would recommend against that. – briantist Jul 07 '16 at 14:06