Reconnect and Resume

Advertisement

Woojo01
Joined:
Posts:
4
Location:
Florida

Reconnect and Resume

I am using Powershell with the .NET assembly to obtain a 7G file on a daily basis. There are occasional times where the download is interrupted for various reasons of which the supplier has not been able to identify. I have used a "for" loop to execute the transfer multiple times after a failure. However, it seems the transfer always starts the from the beginning of the file on each iteration. This is very time consuming especially if the the transfer aborts 45 minutes into the download and starts from the beginning again. I do know the server supports resuming a download.

Am I missing something in the session options to enable an automatic reconnect and resuming a download where it left off?
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 
}    

Reply with quote

Advertisement

You can post new topics in this forum