Powershell WinSCP和定义的端口

Powershell WinSCP和定义的端口,powershell,winscp,winscp-net,Powershell,Winscp,Winscp Net,我正在尝试使用WinSCP连接到我们的ftp。但是如何使用.net程序集在PowerShell中定义端口呢 我正在尝试制作一个解决方案,从服务器下载最近的文件,在服务器上删除,然后将其导入MSSQL数据库 但我现在的问题是使用WinSCP连接到ftp。没有任何代码很难说,但请尝试以下操作: $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp $s

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

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


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

没有任何代码很难说,但请尝试以下操作:

$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.PortNumber = 2222
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"
WinSCP会话选项

WinSCP会话选项默认为SFTP和端口22,因此普通会话对象只需要以下几项

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

上面的代码正在运行并连接到一个真正的服务器

在您的问题中,您最初要求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
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