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

leemerrill
Joined:
Posts:
1
Location:
United States

LoadFrom path

TheBbib

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

Reply with quote

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

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

martin
Site Admin
martin avatar
Joined:
Posts:
29,070
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.
$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

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:

Thanks you for sharing the details. I have documented this on:
https://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

Reply with quote

Advertisement

Johan Nieuwhoff
Guest

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

Johan Nieuwhoff wrote:

martin wrote:

Thanks you for sharing the details. I have documented this on:
https://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"

Reply with quote

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:
29,070
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 https://winscp.net/eng/docs/message_net_operation_not_supported

Reply with quote

Advertisement

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"

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.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
29,070
Location:
Prague, Czechia

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

Thanks for sharing your solution.

Reply with quote

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

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:
$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

Reply with quote

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:
$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.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
29,070
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

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum