Search code examples
powershellcom-interoptlbimp

How do I generate a PowerShell-compatible version of a COM assembly using TLBImp.exe?


I'm doing some work with MSMQ Triggers and Rules from PowerShell, and I'm having an issue.

I know that the only way to access triggers/rules programmatically is to leverage the mqtrig.dll assembly, and my research has indicated that I have to run tlbimp.exe to create a .NET assembly that I can load from PowerShell. Okay, no problem!

Except when I run tlbimp and try to load the assembly it generates, I get this error:

Add-Type : Could not load file or assembly 'file:///C:\users\daniel.mann\desktop\mqtrig.dll' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

Looking at $psversiontable shows me this:

Name                           Value                                                                                                                                                                                                                 
----                           -----                                                                                                                                                                                                                 
CLRVersion                     2.0.50727.5477                                                                                                                                                                                                        
BuildVersion                   6.1.7601.17514                                                                                                                                                                                                        
PSVersion                      2.0                                                                                                                                                                                                                   
WSManStackVersion              2.0                                                                                                                                                                                                                   
PSCompatibleVersions           {1.0, 2.0}                                                                                                                                                                                                            
SerializationVersion           1.1.0.1                                                                                                                                                                                                               
PSRemotingProtocolVersion      2.1    

So, okay. I get it. I need to use a version of tlbimp that generates CLR 2.0 assemblies. Easy, right?

Well, I happen to have some ancient versions of Visual Studio on this box, so I found one:

C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin>tlbimp

Microsoft (R) .NET Framework Type Library to Assembly Converter 2.0.50727.42

Except I get the same error.

I've tried at least 3 different versions of tlbimp, which give versions of 2.0, 3.5, and 4.0. What is the correct version for PowerShell 1.0/2.0, and where would it be located on a development machine?

I know you can create a PowerShell.exe.config file and allow it to load newer assemblies, but that is absolutely not going to fly here -- this is part of an automated deployment scenario, and I'd prefer to not have to go mess with config files as a pre-deployment step.


Solution

  • I got meticulous all up in this problem's face.

    First:

    gci -rec C:\ -filter "tlbimp.exe" That got me all the copies of tlbimp on this box.

    Then, I ran this:

    &"C:\Program Files\Microsoft.NET\SDK\v2.0 64bit\Bin\TlbImp.exe" "C:\windows\system32\mqtrig.dll" /out:"C:\users\daniel.mann\desktop\mqtrig1.dll"
    &"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\TlbImp.exe" "C:\windows\system32\mqtrig.dll" /out:"C:\users\daniel.mann\desktop\mqtrig2.dll"
    &"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\TlbImp.exe" "C:\windows\system32\mqtrig.dll" /out:"C:\users\daniel.mann\desktop\mqtrig3.dll"
    &"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\TlbImp.exe" "C:\windows\system32\mqtrig.dll" /out:"C:\users\daniel.mann\desktop\mqtrig4.dll"
    &"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64\TlbImp.exe" "C:\windows\system32\mqtrig.dll" /out:"C:\users\daniel.mann\desktop\mqtrig5.dll"
    &"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\TlbImp.exe" "C:\windows\system32\mqtrig.dll" /out:"C:\users\daniel.mann\desktop\mqtrig6.dll"
    &"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\x64\TlbImp.exe" "C:\windows\system32\mqtrig.dll" /out:"C:\users\daniel.mann\desktop\mqtrig7.dll"
    &"C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\TlbImp.exe" "C:\windows\system32\mqtrig.dll" /out:"C:\users\daniel.mann\desktop\mqtrig8.dll"
    
    add-type -Path "C:\users\daniel.mann\desktop\mqtrig1.dll"
    add-type -Path "C:\users\daniel.mann\desktop\mqtrig2.dll"
    add-type -Path "C:\users\daniel.mann\desktop\mqtrig3.dll"
    add-type -Path "C:\users\daniel.mann\desktop\mqtrig4.dll"
    add-type -Path "C:\users\daniel.mann\desktop\mqtrig5.dll"
    add-type -Path "C:\users\daniel.mann\desktop\mqtrig6.dll"
    add-type -Path "C:\users\daniel.mann\desktop\mqtrig7.dll"
    add-type -Path "C:\users\daniel.mann\desktop\mqtrig8.dll"
    

    mqtrig1, 2, 5, and 8 all loaded successfully. It looks like the problem was that I was being silly and not using the 64 bit version of tlbimp.