Search code examples
vb.netpowershellpowershell-module

Add type during Import-Module


Is there anyway in PowerShell to Add-Type when importing a module?

I have a custom VB DLL that I'm importing as a module. The DLL has 2 imports in it:

Imports Microsoft.ConfigurationManagement.ManagementProvider
Imports Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

The function that relies on the WqlQueryEngine doesn't work until I do:

Add-Type "C:\Program Files (x86)\Configuration Manager\Console\bin\AdminUI.WqlQueryEngine.dll"

Is there anyway to do this during the import? Or change where PowerShell is looking for an assembly?

Also why do I have to do this? The files the import is relying on are both in the same directory? The first import relies on C:\Program Files (x86)\Configuration Manager\Console\bin\Microsoft.ConfigurationManagement.ManagementProvider.dll.

Why does one work and the other needs to be added?


Solution

  • If you're using a module manifest (.psd1) file for your module (and you should), you can specify a list of required assemblies (RequiredAssemblies = @()). This will cause PowerShell to load them before your own module loads the VB DLL.

    Module manifests are described here in detail.

    As for why one works and the other doesn't: It's hard do say. Assemblies already in the GAC don't need to be specifically loaded, maybe that's the case?