PowerShell : Could not load file or assembly winscp.dll

Advertisement

Thebib
Guest

PowerShell : Could not load file or assembly winscp.dll

Hi to all and happy new year!

I have to create a powershell script to automate file transfer.

To load winscp I try this command:
[Reflection.Assembly]::LoadFrom("winscp\WinSCP.dll") | Out-Null
But I'm getting this error:
Could not load file or assembly 'file:///winscp\WinSCP.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
I check winscp.dll file properties and unblock the file.

Thanks for your help.

Reply with quote

Advertisement

Guest

Possible rights

Right click DLL and make sure there is no "Security" block.

Not sure if this is needed (I renamed WINSCP.dll -> WINSCPnet.dll) – run:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WINSCPnet.DLL /codebase /tlb
Be sure your script is running with admin level rights.

Reply with quote

nollmr
Joined:
Posts:
1

Just wanted to say this worked for me and add a few details. When I first tried to register the assembly I received the same error as above:
Could not load file or assembly 'file:///<path>\WinSCP.dll' or one of its dependencies. Operation is not supported.
I right-clicked on the WinSCP.dll file and chose Properties. There was an "Unblock" button on the General tab so I clicked it and hit Ok.
From a command window run as Administrator, I registered the assembly and it then worked.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WINSCP.DLL /codebase /tlb

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
38,209
Location:
Prague, Czechia

Re: Powershell : Could not load file or assembly winscp.dll

Thanks you for sharing the details. I have documented this on:
Could not load file or assembly ‘file:///…\WinSCPnet.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

But anyway, I was able to register even a blocked assembly on Windows 8. What version of Windows are you using?

Reply with quote

Advertisement

AeroX
Guest

If the .NET DLL is in the same directory as your PowerShell script you can load it using the below method instead of registering the assembly.
$ScriptPath = $(Split-Path -Parent $MyInvocation.MyCommand.Definition)
[Reflection.Assembly]::LoadFrom( $(Join-Path $ScriptPath "WinSCPnet.dll") ) | Out-Null

Reply with quote

Guest

.NET and PowerShell directory not in sync

Hi there!

If you changed the directory within the PowerShell session, the current .NET directory might be different.

You can sync the two worlds with the following command:
[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath
This way you can use relative filename
[Reflection.Assembly]::LoadFrom("WinSCPnet.dll")
@Martin: Thanks for this fantastic piece of software!

-Bruno.

Reply with quote

Johan Nieuwhoff
Guest

Re: Powershell : Could not load file or assembly winscp.dll

martin wrote:

But anyway, I was able to register even a blocked assembly on Windows 8. What version of Windows are you using?
That's because Windows 8 is loaded with PowerShell 3.0. I found out that PowerShell 1.0 is not able to handle it. It also might be something with .NET 4.5 (which is required for PowerShell 3.0).
So, make sure you are using the latest PowerShell.

btw:
I was able to load the WinSCP dll with:
Add-Type -Path "\\pathtodll\WinSCPnet.dll"

Reply with quote

Advertisement

namerg
Guest

Not able to load winscp*.dll

Hello, two things:

1. When I load WinSCP.dll and execute a PowerShell script, I get the following error as a windows pop up:
Incompatible external console protocol version 5
I am loading the dll as
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCP.dll") | Out-Null
and in the command prompt I get the following error message:
WinSCP process terminated with exit code 0 and output "", without creating a log file.

2. When I load the WinSCPnet.dll and execute a PowerShell script I get the following message error in the command prompt:
Could not load file or assembly 'file:///C:\Program Files (x86)\WinSCP\WinSCPnet.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
I am loading the dll as
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCPnet.dll") | Out-Null
or
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
Thanks for your help,

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
38,209
Location:
Prague, Czechia

Re: Not able to load winscp*.dll

namerg wrote:

1. When I load WinSCP.dll and execute a PowerShell script I get the following error as a windows pop up:
Incompatible external console protocol version 5
You need to use the same version of WinSCP .NET assembly and WinSCP.

2. When I load the WinSCPnet.dll and execute a PowerShell script I get the following message error in the command prompt:
Could not load file or assembly 'file:///C:\Program Files (x86)\WinSCP\WinSCPnet.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
See Could not load file or assembly ‘file:///…\WinSCPnet.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

Reply with quote

Nathan_McPherson
Joined:
Posts:
1

Add-Type : Could not load file or assembly

PROBLEM:
I am not able to add the WinSCPnet.dll type to my PowerShell script and would appreciate any advice you could offer to help me resolve this issue. Based on a review of the other posts in this forum, I have removed the block on WinSCPnet.dll in Windows Explorer and I have ensured WinSCP.exe is located in the same folder as the DLL.

MY CODE:
Add-Type -Path "C:\Program Files (x86)\WinSCP\WINSCP.NET\WinSCPnet.dll"
ERROR:
Add-Type : Could not load file or assembly 'file:///C:\Program Files
(x86)\WinSCP\WINSCP.NET\WinSCPnet.dll' or one of its dependencies. Operation is not supported.
(Exception from HRESULT: 0x80131515)
FWIW, I was able to create a C# console application that included a reference to "C:\Program Files (x86)\WinSCP\WINSCP.NET\WinSCPnet.dll" after I manually added WinSCP.exe to the project. This test leads me to believe my problem my be unique PowerShell. But, I'm not sure what else to check.

UPDATE:
I was able to resolve this problem by using a utility called "Streams" from Microsoft's SYSINTERNALS to permanently unblock these two files. This seems a bit extreme; however, it resolved my problem.

Reply with quote

Advertisement

perry99
Guest

Re: Add-Type : Could not load file or assembly

Well, for me worked to create two files
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe.config
with content:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>
For some reason PowerShell thinks, the DLL is remote, even if it is on a local drive, and I didn't find a way to "unblock" the specific file.

Reply with quote

Advertisement

GreenLion
Guest

I ran into the same problem and solved it with another approach.

Mainly I found out that with VS 2013 (PowerShell extension from CodePlex) is not very near to the native PowerShell environment.
So I had to load the assembly differently for each environment.

I handled it like so:
$setDevEnviromentVS = $true
#Load WinSCP assembly according DEV Enviroment
if($setDevEnviromentVS)
{
    [System.Reflection.Assembly]::LoadFrom('.\_components\winscp572\WinSCPnet.dll')
}
else
{
    Add-Type -Path '\\<UNC>Apps\winscp572\WinSCPnet.dll'
}
so with this code - you can run the script either from VS or from ISE.

HTH

Reply with quote

Guest

I was trying to execute from network drive and kept on getting this error.
I none of the above solution did not work for me, although from your coding, I just changed it to
[System.Reflection.Assembly]::UnsafeLoadFrom('.\_components\winscp572\WinSCPnet.dll')
This UnsafeLoadFrom did the trick for me.
below is the copy from MSDN.

If an application has been copied from the web, it is flagged by Windows as being a web application, even if it resides on the local computer. You can change that designation by changing the file properties, or you can use the <loadFromRemoteSources> element to grant the assembly full trust. As an alternative, you can use the UnsafeLoadFrom method to load a local assembly that the operating system has flagged as having been loaded from the web.

Reply with quote

Guest

Working with multiple machines I had downloaded WinSCP .NET assembly from one and extracted files on other. I guess because of this Windows did not react to "Unblock" (this option would not disappear). So I downloaded assembly form the same machine and for those files unblock worked with the first time.

Reply with quote

Advertisement

You can post new topics in this forum