Brendan Lovett
Brendan Lovett

Reputation: 43

Unable to load a .NET assembly in PowerShell 7

mRemoteNG is a multi-remote protocol connection client, comparable to RDCMan, but with support for other protocols like SSH, VNC, etc.

I'm making a custom version of the mRemoteNG Create Bulk Connections script. The script is written for PS 5.1, I am attempting to update it to 7 along with a number of other components within the script. The script creates connections in bulk via PS so that it does not need to be done via the UI (a giant pain in the ass if you have a lot to add).

To create each connection object, a strongly-typed .NET object is built via mRemoteNG.exe which is later serialized and exported to an XML. I am attempting to perform the first step: load mRemoteNG.exe in the current session to create strongly-typed .NET objects further down the pipeline.

In 5.1, I am able to load mRemoteNG.exe without issue using [System.Reflection.Assembly]::LoadFile(). In 7, this same method does not work. I understand that the above method will eventually be deprecated and thus switched over to Add-Type hoping to resolve the issue, but got the same result.

To accurately test the below, you need to download mRemoteNG here. I am using v1.77 on my machine and would suggest you do the same to achieve the most accurate reproduction of this issue. However, I have tested this against 1.76 and can confirm the same issue occurs.

What I am running:

The error I receive upon attempting to load using either of the above methods:

Exception calling "LoadFile" with "1" argument(s): "Could not load file or assembly 'mRemoteNG, Version=1.77.0.41252, Culture=neutral, PublicKeyToken=null'."

PublicKeyToken=null initially led me to believe there was a signing issue, but Googling around seems to indicate this is a dependency conflict. In this case, though, no dependency issue is indicated in the exception as it typically would be.

It is worth noting that mRemoteNG requires .NET 4.6. Running [System.Reflection.Assembly]::GetExecutingAssembly().ImageRuntimeVersion in PS 5.1 and 7 indicates the default on my system is 4.0.30319. I am struggling to understand how PS running with the same .NET version in 5.1 and 7 produces two completely different results.

Does anyone have any insight as to why this is occurring and how it can be resolved?

Upvotes: 0

Views: 1328

Answers (1)

Brendan Lovett
Brendan Lovett

Reputation: 43

This turned out to be an issue with the PowerShell architecture. Note that mRemoteNG is stored in the Program Files (x86) directory, it is a 32-bit application. I was attempting to run it in a 64-bit PowerShell session, thus it would not function as expected. The error message was incredibly unhelpful in determining this, so thank you to the folks over at reddit.com/r/powershell for being a second set of eyes!

Perhaps the oddest part of this issue is the fact that mRemoteNG binaries will run in Windows PowerShell 5.1 x64, but not in PowerShell Core 7.0.3 x64. It must be run in PowerShell Core 7.0.3 x86. This inconsistent behavior is what threw me off originally and made me not even consider trying the x86 version of PowerShell 7.0.3.

Upvotes: 2

Related Questions