This is an old revision of the document!
Using WinSCP .NET Assembly via COM and from WSH-hosted Languages
Advertisement
Registering for COM
WinSCP .NET assembly exposes its full interface to COM. As a COM library, it needs to be registered before use. To register the assembly use:
%WINDIR%\Microsoft.NET\Framework\<version>\RegAsm.exe WinSCP.dll /codebase /tlb
Where the %WINDIR%
is path to your Windows installation, what is typically C:\Windows
or C:\WINNT
. Note that you can keep %WINDIR%
as this environment variable should be set on your system to point to the Windows folder. The <version>
is version of .NET framework to register the assembly with. It is recommended to use the latest available, what currenly is v4.0.30319
. You may however use any framework version from 2.0 (v2.0.50727
) up. Note that framework 3.0 and 3.5 do not ship with RegAsm.exe
. For these versions use RegAsm.exe
from 2.0.
Typical registration command for .NET 4.0 would be:
%WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCP.dll /codebase /tlb
Using from WSH
You use WinSCP .NET assembly from WSH as any other COM library.
Though there are some less known techniques that you may need to use, which are described in following sections.
Accessing Enumeration Values
As JScript and VBScript access COM classes via IDispatch
, they do not make use of type library, hence they do not have direct access to constants defined there, like Protocol.Sftp
for instance. To use these, you have to instruct WSH to import the type library into the script namespace.
Advertisement
You can use Windows Script File (WSF) and its <reference>
tag for that. It makes WSH convert all enums in the assembly interface into constants with name like <type>_<member>
, e.g. Protocol.Sftp
becomes Protocol_Sftp
.
<job> <reference object="WinSCP.Session"/> <script language="JScript"> var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions"); sessionOptions.Protocol = Protocol_Sftp; sessionOptions.HostName = "example.com"; sessionOptions.UserName = "user"; sessionOptions.Password = "mypassword"; sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; var session = WScript.CreateObject("WinSCP.Session"); session.Open(sessionOptions); session.PutFiles("d:\\toupload\\*", "/home/user/"); </script> </job>
You run the .wsf
file the same way as you run .js
or .vbs
:
cscript upload.wsf
Event Handlers
The Session
class exposes several events.
If you need to make use of these events:
- Use overload of
CreateObject
with 2 arguments, passing an unique prefix as the second argument (e.g. “session_”); - Define function with name
<prefix><event>
and two arguments (e.g.sender
ande
) for every event you need to handle.
See following JScript snippet:
function session_FileTransferred(sender, e) { WScript.Echo(e.FileName + " => " + e.Destination); } var session = WScript.CreateObject("WinSCP.Session", "session_");
Advertisement
JScript Example
This example is functionally equivalent to overall C# example for WinSCP .NET assembly.
<job> <reference object="WinSCP.Session"/> <script language="JScript"> // Setup session options var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions"); sessionOptions.Protocol = Protocol_Sftp; sessionOptions.HostName = "example.com"; sessionOptions.UserName = "user"; sessionOptions.Password = "mypassword"; sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; var session = WScript.CreateObject("WinSCP.Session"); // Connect session.Open(sessionOptions); // Upload files var transferOptions = WScript.CreateObject("WinSCP.TransferOptions"); transferOptions.TransferMode = TransferMode_Binary; var transferResult = session.PutFiles("d:\\toupload\\*", "/home/user/", false, transferOptions); // Throw on any error transferResult.Check(); // Print results for (var enumerator = new Enumerator(transferResult.Transfers); !enumerator.atEnd(); enumerator.moveNext()) { WScript.Echo("Upload of", enumerator.item().FileName, "succeeded"); } </script> </job>