Topic "Powershell : Could not load file or assembly winscp.dll"

Author Message
Thebib

Guest


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.
Advertisements
leemerrill
[View user's profile]

Joined: 2013-01-16
Posts: 1
Location: United States
TheBbib

Try this.
[Reflection.Assembly]::LoadFrom("\\c:\Program Files (x86)\WinSCP\WinSCP.dll") | Out-Null
Guest




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
nollmr
[View user's profile]

Joined: 2013-09-19
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:

Code:
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.
Code:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WINSCP.DLL /codebase /tlb
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Thanks you for sharing the details. I have documented this on:
http://winscp.net/eng/docs/message_net_operation_not_supported

But anyway, I was able to register even a blocked assembly on Windows 8. What version of Windows are you using?
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.
Code:
$ScriptPath = $(Split-Path -Parent $MyInvocation.MyCommand.Definition)
[Reflection.Assembly]::LoadFrom( $(Join-Path $ScriptPath "WinSCPnet.dll") ) | Out-Null
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
AeroX wrote:
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.
Code:
$ScriptPath = $(Split-Path -Parent $MyInvocation.MyCommand.Definition)
[Reflection.Assembly]::LoadFrom( $(Join-Path $ScriptPath "WinSCPnet.dll") ) | Out-Null

You never need to register the assembly to load it to PowerShell.
_________________
Martin Prikryl
Guest




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.
Johan Nieuwhoff

Guest


prikryl wrote:
Thanks you for sharing the details. I have documented this on:
http://winscp.net/eng/docs/message_net_operation_not_supported

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 u are using the latest powershell
Johan Nieuwhoff

Guest


Johan Nieuwhoff wrote:
prikryl wrote:
Thanks you for sharing the details. I have documented this on:
http://winscp.net/eng/docs/message_net_operation_not_supported

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 u are using the latest powershell


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

Guest


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
Code:
[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
Code:
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCPnet.dll") | Out-Null
or
Quote:
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"


Thanks for your help,
namerg

Guest


I am all set. Thanks....
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
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.

Quote:
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 http://winscp.net/eng/docs/message_net_operation_not_supported
Nathan_McPherson
[View user's profile]

Joined: 2015-06-27
Posts: 1
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"

ERRROR:
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.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Thanks for sharing your solution.
perry99

Guest


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.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
perry99 wrote:
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.

As already suggested above, see
http://winscp.net/eng/docs/message_net_operation_not_supported
FilipHeens

Guest


Hi This eventually helped me to run the winscp via powershell Very Happy

http://fczaja.blogspot.com/2012/06/add-pssnapin-cannot-load-windows.html

hope this helps someone
GreenLion

Guest


hi guys,

i ran into the same prolbem 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 enviroment.
so i had to load the assembly differently foreach enviroment.

i handled it like so:
Code:

$setDevEnviromentVS = $true
#Load WinSCP assembly according DEV Enviroment
   if($setDevEnviromentVS)
   {
      [System.Reflection.Assembly]::LoadFrom('.\_components\winscp572\WinSCPnet.dll')
   }
   else
   {
      dd-Type -Path '\\<UNC>Apps\winscp572\WinSCPnet.dll'
   }


so with this code - you can run the script either from VS or from ISE.

HTH
Guest




GreenLion wrote:
hi guys,

i ran into the same prolbem 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 enviroment.
so i had to load the assembly differently foreach enviroment.

i handled it like so:
Code:

$setDevEnviromentVS = $true
#Load WinSCP assembly according DEV Enviroment
   if($setDevEnviromentVS)
   {
      [System.Reflection.Assembly]::LoadFrom('.\_components\winscp572\WinSCPnet.dll')
   }
   else
   {
      dd-Type -Path '\\<UNC>Apps\winscp572\WinSCPnet.dll'
   }


so with this code - you can run the script either from VS or from ISE.

HTH



hi, experts

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.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Anonymous wrote:
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.


This is covered here:
https://winscp.net/eng/docs/message_net_operation_not_supported
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 disapear). So I downloaded assembly form the same machine and for those files unblock worked with the first time.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License