Re: Reconnect and Resume
The WinSCP .NET assembly does not support resuming yet.
https://winscp.net/tracker/834
https://winscp.net/tracker/834
Function Get-SupplierFtpFile
{
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$True)]
[String]$RemotePath,
[Parameter(Position=1, Mandatory=$True)]
[String]$LocalPath,
[Parameter(Position=4, Mandatory=$false)]
[int]$Iterations = 36,
[Parameter(Position=5, Mandatory=$false)]
[int]$WaitSeconds = 300
)
Try
{
[Void][System.Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCP.dll")
$FileName = [System.IO.Path]::GetFileName($RemotePath)
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
$sessionOptions.FtpMode = [WinSCP.FtpMode]::Passive
$sessionOptions.HostName = "ftp4.xxxxxxxx.com"
$sessionOptions.UserName = "Username"
$sessionOptions.Password = "pa$$word"
$sessionOptions.PortNumber = 990
$sessionOptions.FtpSecure = [WinSCP.FtpSecure]::Implicit
$sessionOptions.SslHostCertificateFingerprint = "99:98:97:96:19:2f:dd:cc:aa:ff:89:78:77:66:76:75:74:73:72:71"
# Connect
$session = New-Object WinSCP.Session
$session.SessionLogPath = "D:\Logs\GetFTP\$(Get-Date -Format "yyyyMMddHHmm").FTPlog.txt"
$session.Open($sessionOptions)
Try
{
For([int]$x = 1;$x -le $Iterations;$x += 1)
{
Write-EventLog -LogName ScriptEvents -Source PowerShellITG `
-Message "Supplier refresh: Download started." `
-EventId 1001 -EntryType Information
Try
{
$session.GetFiles($remotePath,$localPath).Check()
Break
}
Catch
{
Write-Warning $_.Exception.Message
Write-EventLog -LogName ScriptEvents -Source PowerShellITG `
-Message "Supplier refresh: Download aborted $x times. Retry in 5 minutes." `
-EventId 1002 -EntryType Warning
}
Start-Sleep -Seconds $WaitSeconds
}
If($x -ge $Iterations)
{
Throw "Excessive time waiting for remote file access."
}
Get-Item $localPath -ErrorVariable ERR -ErrorAction SilentlyContinue | Out-Null
If($ERR)
{
Throw $ERR.Message
}
Return $localPath
}
Catch
{
Throw $_.Exception
}
}
Finally
{
# Disconnect, clean up
$session.Dispose()
}
Return
}