PowerShell runtime exception - "could not load file or assembly"
Asked Answered
R

5

15

This seems to be a common problem in PowerShell and Visual Studio, yet cases and solutions seem to vary a lot. Though seeing several similar questions, I didn't find a working solution for my issue yet.

The problem exists in the error message

Could not load file or assembly 'file:///C:\users\jenstmar\Desktop\WinSCP.dll' o r one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

The file location is invalid, as the .dll is supposed to be located in the same folder as the WinSCP installation. This location was changed to check that no rights or lack thereof, restricted me to use it.

The script line that causes the problem looks as following:

# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("C:\users\jenstmar\Desktop\WinSCP.dll") | Out-Null

This error comes in both PowerShell ISE and PS ISE(x86). I run PowerShell V3.0 in a Windows Enterprise 64 bit environment as local administrator. How can I fix this problem?

Renie answered 10/12, 2012 at 10:39 Comment(0)
R
11

Solution in this case: Remove and download it all again. I even unblocked it on the first file, with no help. Glad this was on an internal machine.

Renie answered 13/12, 2012 at 9:18 Comment(4)
Who thought it will be the solution. :) I've been trying for about 6 hours now. Thanks Mark JensterSarnen
If PowerShell ISE was open at time of unblocking, close and reopen it. It seems to lock the DLL in certain scenarios delaying the unblock action from happening.Lavatory
This is a great solutionCutout
I think for me the file was blocked by VS Code; after killing the process 'pwsh.exe' and overwriting it, it worked.Calumniate
O
18

The file may be blocked from being loaded because of its origin (stored in NTFS streams). Check properties and see if the little unblock button is lit up. Once you unblock it perhaps it will load...

Ornelas answered 10/12, 2012 at 16:11 Comment(3)
Also documented here: winscp.net/eng/docs/message_net_operation_not_supportedAcetic
Turns out my vm's user directory was on a shared drive. I had to first download the file to the local pc, then remove the lock.Loy
Once the file is unblocked, close and open the powershell again otherwise it will not work.Shawanda
R
11

Solution in this case: Remove and download it all again. I even unblocked it on the first file, with no help. Glad this was on an internal machine.

Renie answered 13/12, 2012 at 9:18 Comment(4)
Who thought it will be the solution. :) I've been trying for about 6 hours now. Thanks Mark JensterSarnen
If PowerShell ISE was open at time of unblocking, close and reopen it. It seems to lock the DLL in certain scenarios delaying the unblock action from happening.Lavatory
This is a great solutionCutout
I think for me the file was blocked by VS Code; after killing the process 'pwsh.exe' and overwriting it, it worked.Calumniate
S
4

The following worked for me (from Stack Overflow question Add-Type load assembly from network UNC share error 0x80131515).

In files:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>
Scion answered 20/1, 2014 at 22:41 Comment(0)
K
2
Import-Module : Could not load file or assembly 'file:/// *dll path*' or one of 
its dependencies. An attempt was made to load a program with an incorrect format.
At *script path*.ps1:68 char:2
+     Import-Module *module path*
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], BadImageFormatException
    + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.ImportModuleCommand

If you see the above error, it might be the issue of running the Windows PowerShell ISE in 32 bit mode(Windows PowerShell ISE(x86)). You have to run the application Windows PowerShell ISE.

This application(Windows PowerShell ISE) is in 64 bit mode. When I executed the power shell command, its working fine for me.

I have been trying to fix this with different types solutions but it doesn't worked for me. If you running it 32 mode, switch it to 64 bit and try.

Kept answered 25/10, 2016 at 9:46 Comment(0)
P
0

I would look at WinSCP.dll dependencies in ILDASM or dotPeek and then load those dependencies before loading WinSCP.dll. The problem is that you are running in PowerShell.exe (or powershell_ise.exe) fusion load context and the assemblies that WinSCP.dll aren't going to be found under these two exe's base dirs. So you will need to load the dependent assemblies before the CLR loader chokes because it can't find a required DLL. If you need help figuring out which assembly can't be found, check out the fuslogvw.exe tool.

BTW, as of PowerShell v2 you should use Add-Type -Path <path> instead of [S.R.A]::LoadFrom(...).

Potency answered 10/12, 2012 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.