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.
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 - HAL9256Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
- somebadhat