WinSCP和定义的端口

时间:2014-08-13 10:23:45

标签: powershell winscp winscp-net

我正在尝试使用WinSCP连接到我们的ftp。但是如何使用.net程序集在PowerShell中定义端口!

我正在尝试制作一个解决方案,从服务器下载最新文件,在服务器上删除它,然后将其导入MSSQL数据库。

但我现在的问题是使用WinSCP连接到ftp。

2 个答案:

答案 0 :(得分:1)

没有任何代码很难说,但尝试这样的事情:

$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.PortNumber = "22"
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"

答案 1 :(得分:1)

<块引用>

WinSCP 会话选项 LINK

WinSCP 会话选项默认为 SFTP 和 PORT 22,因此普通会话对象只需要像这样的一些东西...

<块引用>

注意:我将在 Visual Basic 6 中向以这种方式使用库时遇到问题的任何人显示,但 VB.net、C# 和 PowerShell 的逻辑与问题类似问。

Dim sessionOptions
Set sessionOptions = CreateObject("WinSCP.SessionOptions")

With sessionOptions
 .HostName = SftpUrl
 .UserName = UserName
 .Password = UserPassword
 .SshHostKeyFingerprint = "ssh-rsa 2048 Ue9.................................="
End With

Dim sftpSession
Set sftpSession = CreateObject("WinSCP.Session")

On Error GoTo YOURERROR
sftpSession.open sessionOptions

If sftpSession.opened Then
    'Do stuff...
End If
<块引用>

以上代码正在运行并连接到真实服务器。

在您的问题中,您最初要求使用 FTP,但您确实正确并说 SFTP。但是,我也会显示一个 FTP 请求,因为 WinSCP 支持它,并且示例至少需要设置协议。

WinSCP根据使用的协议设置端口,所以在下面的例子中我们仍然不需要设置端口。

如果端口与所用协议的默认服务器端口不同,则只需设置端口。

Dim sessionOptions
Set sessionOptions = CreateObject("WinSCP.SessionOptions")

With sessionOptions
 .Protocol = Protocol_Ftp
 .HostName = FtpUrl
 .UserName = UserName
 .Password = UserPassword
End With

Dim FtpSession
Set FtpSession = CreateObject("WinSCP.Session")

On Error GoTo YOURERROR
FtpSession.open sessionOptions

If FtpSession.opened Then
    'Do stuff...
End If