3

I am trying to connect to our ftp using WinSCP. But how can i define a port in PowerShell using the .net assembly!

I am trying to make a solution where I download the recent files from the server, delete it on the server and then import it to a MSSQL Database.

But my issue now is connecting to the ftp using WinSCP.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Kwire
  • 107
  • 1
  • 4
  • 12

2 Answers2

3

It's hard to say without any code but try something like this:

$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.PortNumber = 2222
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user3316995
  • 100
  • 2
  • 8
1

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
SoEzPz
  • 14,958
  • 8
  • 61
  • 64