0
votes

I am trying to construct a Powershell script that leverages the WinSCP binaries to download files from an FTP server.

The script so far is as follows (minus actual IPs and folder paths):

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    FtpMode = [WinSCP.FtpMode]::Passive
    HostName = "ftp server ip address"
    UserName = "ftp-username"
    Password = "ftp-password"
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Download files
    $session.GetFiles("/home/ftp-username/uploads/*.txt", "C:\temp\").Check()
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}    

I keep on getting the following error:

Exception calling "Open" with "1" argument(s): "Connection failed. Timeout detected. (control connection) Connection failed." At C:\winscp-ftp.ps1:18 char:5 + $session.Open($sessionOptions) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SessionRemoteException

I did some reading and suggestions say that FTPMode Passive will solve this, but even after including that, the error persists.

Any advice/guidance will be appreciated.

1
There's a complete PS module at the PSGallery for this: powershellgallery.com/packages/Posh-SSH/2.0.2 - Scepticalist
The issue is with your session options. i.e. try connecting manually with the same settings. Timeout means that you can't establish the connection so a setting is wrong, e.g. , Passive/Implicit or FTP/SFTP/SSH/FTPS, or accepting certificate, or TLS 1.2. Match whatever your manual connection settings are successful with your config - HAL9256
i can't even get to your error without Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" - somebadhat
Can you connect anyhow? E.g. in WinSCP GUI? Is it FTP or SFTP? Those are two completely different protocols. And you have used both tags. - Martin Prikryl
@MartinPrikryl the GUI connects perfectly. - trevoirwilliams

1 Answers

3
votes

Windows 10 64-bit. Winscp v.5.1

I was getting the same error message. I used WinSCP to generate the script and it started working.

My guess, and it is a weak one at that, is your hostname and/or username and/or password is malformed. If you use WinSCP to generate the script it has a "copy to clipboard" function.

This works on my ftp server. I don't know why. My code is hardly different than yours. Could it be whitespace after hostname, username, password?

Use the full path to winscpnet.dll

I don't think it makes any difference whether you use $sessionOptions.AddRawSettings("ProxyPort", "0")

It worked whether I did passive or not, which is odd because I always have to use passive.

I connected to my ftp server and downloaded files to c:\temp

I installed Posh-SSH 2.2 but the error message continued. I un-installed Posh-SSH 2.2 before getting the script to work.

If you use WinSCP to generate the code it does not tell you to use the full path to winscpnet.dll

It works after logging off and on.
It works after a restart.
It works in a window w/ or w/o admin privileges.

# Load WinSCP .NET assembly.  If you are not in the winscp directory use the full path.
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    FtpMode = [WinSCP.FtpMode]::Passive
    HostName = "ftp server ip address"
    UserName = "ftp-username"
    Password = "ftp-password"
}

$sessionOptions.AddRawSettings("ProxyPort", "0")

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Download files
$session.GetFiles("/home/ftp-username/uploads/*.txt", "C:\temp\").Check())
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}