We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x
Private

Register a WinSCPnet.dll with PowerShell on a server

RIAS
RIAS asked
on
High Priority
73 Views
Last Modified: 2020-04-16
Hello,
How do I Register a WinSCPnet.dll with PowerShell on a server.

Regards
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
So that you can use it in a script?
All you need to do is tell PowerShell to import the dll at the beginning of the script:
Add-Type -Path "<WinSCP Folder>\WinSCPnet.dll"

Open in new window

Check here for details:
Using WinSCP .NET Assembly from PowerShell
https://winscp.net/eng/docs/library_powershell

Author

Commented:
So when I open powershell , I need to just type

Add-Type -Path "WinSCP\WinSCPnet.dll"

Author

Commented:
Getting this error:
PS C:\Users\admin.nu> Add-Type -Path "WinSCP_Automation\WinSCPnet.dll"
Add-Type : Cannot bind parameter 'Path' to the target. Exception setting "Path": "Cannot find path
'C:\Users\admin.nu\WinSCP_Automation\WinSCPnet.dll' because it does not exist."
At line:1 char:16
+ Add-Type -Path "WinSCP_Automation\WinSCPnet.dll"
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Add-Type], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.AddTypeCommand
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
With the correct path to wherever the dll is actually stored, obviously.
But after that, yes, you'll have the functions of the assembly at your disposal.

Author

Commented:
My WinSCPnet.dll is in the folder
C:\WinSCP_Automation\WinSCPnet.dll
Any suggestion ?

Author

Commented:
Should it be :
C:\Users\admin.nu> Add-Type -Path "C:\WinSCP_Automation\WinSCPnet.dll"
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Just provide the full path; if you don't have a drive, PS will use the path relative to the current directory
Add-Type -Path "C:\WinSCP_Automation\WinSCPnet.dll"

Open in new window

Author

Commented:
PS C:\Users\admin.nu> Add-Type -Path "C:\WinSCP_Automation\WinSCPnet.dll"

Add-Type : Could not load file or assembly 'file:///C:\WinSCP_Automation\WinSCPnet.dll' or one of its dependencies.
Operation is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Add-Type -Path "C:\WinSCP_Automation\WinSCPnet.dll"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

Author

Commented:
Not sure I am missing anything here
Hello ThereSystem Administrator
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Use the command line:
cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 (framework version)
RegAsm.exe ""C:\WinSCP_Automation\WinSCPnet.dll" 

Open in new window

Author

Commented:
C:\>RegAsm.exe "C:\Users\admin.nu\WinSCP_Automation\WinSCP.dll"
'RegAsm.exe' is not recognized as an internal or external command,
operable program or batch file.

Author

Commented:
Basically its a server I am trying to register the dll on .
Hello ThereSystem Administrator
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
You skipped the first line of the code.

Author

Commented:
C:\>cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 (framework version)
The system cannot find the path specified.

C:\>RegAsm.exe "C:\Users\admin.nu\WinSCP_Automation\WinSCP.dll"
Hello ThereSystem Administrator
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Another try:
%systemroot%\SysWoW64\regsvr32 "C:\Users\admin.nu\WinSCP_Automation\WinSCP.dll"

Open in new window

Author

Commented:
Thanks, can't do cd..
to go to c: on command prompt
Hello ThereSystem Administrator
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Open the file explorer and browse to C:\WINDOWS\Microsoft.NET\Framework\ -> you can see some folders named as .NET framework versions.

Then open CMD:
C:\WINDOWS\Microsoft.NET\Framework\<type here the folder name with the highest number>\RegAsm.exe "C:\WinSCP_Automation\WinSCPnet.dll"

Open in new window

Author

Commented:
ok thanks ,
the earlier
%systemroot%\SysWoW64\regsvr32 "C:\Users\admin.nu\WinSCP_Automation\WinSCP.dll" failed as module not found .
But will try the new suggestion.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
There is no need to "register" that dll anywhere.
I just copied the WinSCPnet.dll (v5.17.3.0) to C:\Temp on a server that doesn't have WinSCP installed, imported it, and it worked as it should, without further ado:
PS C:\> Add-Type -Path C:\Temp\WinSCPnet.dll
PS C:\> New-Object WinSCP.SessionOptions


Name                                         : session
Protocol                                     : Sftp
HostName                                     :
PortNumber                                   : 0
UserName                                     :
Password                                     :
SecurePassword                               :
NewPassword                                  :
SecureNewPassword                            :
Timeout                                      : 00:00:15
TimeoutInMilliseconds                        : 15000
PrivateKeyPassphrase                         :
SecurePrivateKeyPassphrase                   :
RootPath                                     :
SshHostKeyFingerprint                        :
GiveUpSecurityAndAcceptAnySshHostKey         : False
SshPrivateKeyPath                            :
SshPrivateKeyPassphrase                      :
FtpMode                                      : Passive
FtpSecure                                    : None
WebdavSecure                                 : False
WebdavRoot                                   :
TlsHostCertificateFingerprint                :
GiveUpSecurityAndAcceptAnyTlsHostCertificate : False
TlsClientCertificatePath                     :



PS C:\>

Open in new window

So which version of Windows are you trying to run this on, and which version of the .NET framework is installed?
Which version of the WinSCP DLL is this, and how did you obtain it?

Author

Commented:
server v.Win10

NET framework is installed : 4.8

 WinSCP DLL : 1.6.5.10071

Author

Commented:
oBdA,
Thanks for that but how do you manage to get


PS C:\> Add-Type -Path C:\Temp\WinSCPnet.dll

How to get rid of Users\admin.nu in

PS C:\Users\admin.nu> Add-Type -Path "C:\WinSCP_Automation\WinSCPnet.dll"

Author

Commented:
Hi,
C:\>cd C:\WINDOWS\Microsoft.NET\Framework\4.0.30319\RegAsm.exe "C:\WinSCP_Automation\WinSCPnet.dll"
The filename, directory name, or volume label syntax is incorrect.
Thanks
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
The part in front is just the prompt, that's the folder where you currently are (you can change that with "Set-Location" or "cd").
PS C:\Users\admin.nu> Add-Type -Path "C:\WinSCP_Automation\WinSCPnet.dll"
It has absolutely no impact if you provide an absolute path to the DLL.
PS C:\Users\Administrator> Add-Type -Path C:\Temp\WinSCPnet.dll
PS C:\Users\Administrator>

Open in new window

So re-copy that dll from a known good source, or install the latest version on a test machine and copy the DLL from there.
What's the output of
Get-Item "C:\WinSCP_Automation\WinSCPnet.dll" | select -expa VersionInfo

Open in new window

Author

Commented:
The output is
PS C:\> Get-Item "C:\WinSCP_Automation\WinSCPnet.dll" | select -expa VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
5.15.9.0         1.6.5.10071      C:\WinSCP_Automation\WinSCPnet.dll

Author

Commented:
Next one is this :

PS C:\> Add-Type -Path C:\WinSCP_Automation\WinSCPnet.dll
Add-Type : Could not load file or assembly 'file:///C:\WinSCP_Automation\WinSCPnet.dll' or one of its dependencies.
Operation is not supported. (Exception from HRESULT: 0x80131515)
At line:1 char:1
+ Add-Type -Path C:\WinSCP_Automation\WinSCPnet.dll
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

Author

Commented:
HelloThere,


C:\>cd C:\WINDOWS\Microsoft.NET\Framework\4.0.30319\RegAsm.exe "C:\WinSCP_Automation\WinSCPnet.dll"
The filename, directory name, or volume label syntax is incorrect.

Also tried

C:\>cd C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe "C:\WinSCP_Automation\WinSCPnet.dll"
The filename, directory name, or volume label syntax is incorrect.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Again: Install the latest version (v5.17.3) on a test machine and copy the DLL from there to the machine in question.
Just tested on Windows 10 1909 as well as a Server 2016, both without a local WinSCP installation, and it worked without any problem in both cases.

Author

Commented:
Sure, just wanted to confirm the site for download
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Home page is here: https://winscp.net/

Author

Commented:
Cant figure out how to change the destination folder in the exe set up
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Start as Administrator -> Install for all users (recommended) -> Accept License -> Custom installation -> Select Destination Location

Author

Commented:
Trying this now

Get-Item "C:\WinSCP_Automation\WinSCPnet.dll" | select -expa VersionInfo

Author

Commented:
First part:
PS C:\> Get-Item "C:\WinSCP_Automation\WinSCPnet.dll" | select -expa VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
5.17.3.0         1.7.2.10325      C:\WinSCP_Automation\WinSCPnet.dll

Author

Commented:
Second part

PS C:\> Add-Type -Path "C:\WinSCP_Automation\WinSCPnet.dll"
PS C:\>

nothing returned?
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
That's as it should be.
Now try
New-Object WinSCP.SessionOptions

Open in new window

Author

Commented:
oki

Author

Commented:
PS C:\> New-Object WinSCP.SessionOptions
New-Object : Cannot find type [WinSCP.SessionOptions]: verify that the assembly containing this type is loaded.
At line:1 char:1
+ New-Object WinSCP.SessionOptions
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Did you do that in the same PS console where you entered the Add-Type command before?
Open a new PS console, enter
Add-Type -Path "C:\WinSCP_Automation\WinSCPnet.dll"
New-Object WinSCP.SessionOptions

Open in new window

Author

Commented:
No still the same
Don't know ??

Author

Commented:
Only one change while install I did :
I installed it in C:\WinSCP_Automation folder but after install there was one more folder created with files like
C:\WinSCP_Automation\WinSCP\WinSCPnet.dll

So I copied all the files from WinSCP To WinSCP_Automation

Don't know if that matters

Author

Commented:
Hi,
On the same window :
>> New-Object WinSCP.SessionOptions


Name                                         : session
Protocol                                     : Sftp
HostName                                     :
PortNumber                                   : 0
UserName                                     :
Password                                     :
SecurePassword                               :
NewPassword                                  :
SecureNewPassword                            :
Timeout                                      : 00:00:15
TimeoutInMilliseconds                        : 15000
PrivateKeyPassphrase                         :
SecurePrivateKeyPassphrase                   :
RootPath                                     :
SshHostKeyFingerprint                        :
GiveUpSecurityAndAcceptAnySshHostKey         : False
SshPrivateKeyPath                            :
SshPrivateKeyPassphrase                      :
FtpMode                                      : Passive
FtpSecure                                    : None
WebdavSecure                                 : False
WebdavRoot                                   :
TlsHostCertificateFingerprint                :
GiveUpSecurityAndAcceptAnyTlsHostCertificate : False
TlsClientCertificatePath                     :

Author

Commented:
Basically, I am trying to run SSIS Package on a server by SSMS.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks.

Author

Commented:
Hi oBdA,
Having an issue now of GAC version and the new version.
Please give me 2 min will provide the details

Author

Commented:
Yes, definitely, the versions on GAC and the new ones are not matching .

Author

Commented:
Hello,
Having trouble with this command
Set-Alias

Name:Gacutil

Value: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\gacutil.exe

Gacutil /i "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

Author

Commented:
Error:
PS C:\> Gacutil /i "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
Gacutil : The term 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\Bin\gacutil.exe' is not recognized as the
name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:1
+ Gacutil /i "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program File...Bin\gacutil.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Sorry, I can tell you how to use WinSCP in PowerShell (which seems to be working), but I have no idea what it is you want to accomplish right now.

Author

Commented:
Ok thanks. Will close the question. Really appreciate your help.

Author

Commented:
Amazing help provided! Really appreciated.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.