2

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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Shovers_
  • 497
  • 2
  • 13

2 Answers2

2

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?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Trondh
  • 3,221
  • 1
  • 25
  • 34
1

Your powershell module should have a manifest that lists the dependencies.

The important lines from the linked page are:

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()
Eris
  • 7,378
  • 1
  • 30
  • 45