WinSCP SessionOptions LINK
WinSCP Session Options default to SFTP and PORT 22 so a normal session object just needs a few things like this...
Note: I'm going to display in Visual Basic 6 for anyone having trouble using the library in this manner, but the logic is similar for VB.net, C#, and PowerShell as the question asks.
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
The above code is working and connecting to a real server.
In your question, you originally asked for FTP, although you did correct and say SFTP. However, I'll also display a FTP request too as WinSCP supports it and the example WOULD need to set at least the protocol.
WinSCP sets the port based on the protocol used, so in the below example we still do not need to set the port.
One would only need to set the port if it differed from default server ports for the protocol used.
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