Sporadic error code 2 while getting files from SFTP server

Advertisement

Arcplace
Guest

Sporadic error code 2 while getting files from SFTP server

We are using a PowerShell script that is downloading documents from an SFTP server. This was running for quite a long time without any issue, but recently we are running into sporadic error code 2 stating, the file does not exist...? Any idea what could be the issue here?

Script:
param([string] $hostname,
    [string] $username,
    [string] $password,
    [string] $source,
    [string] $destination
)
 
# Set log file
$scriptname = ([System.IO.FileInfo]$MyInvocation.MyCommand.Definition).BaseName
$logfile = "$PSScriptRoot\$scriptname.log"
 
# Log function
Function LogWrite
{
    param([string] $logstring)
 
    Add-Content $logfile -value $logstring
}
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = $hostname
        PortNumber = 2222
        UserName = $username
        Password = $password
        SshHostKeyFingerprint = "*******"
    }
 
    # Create session
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Find files older than 3 minutes
        $directoryInfo = $session.ListDirectory($source)
        $limit = (Get-Date).AddMinutes(-5)
        $readyFiles =
            $directoryInfo.Files |
            Where-Object { -Not $_.IsDirectory } | 
            Where-Object { $_.LastWriteTime -lt $limit }
        
        # Set transfer options
        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
        $transferOptions.OverwriteMode = [WinSCP.OverwriteMode]::Overwrite
        
        foreach ($readyFile in $readyFiles)
        {
            # Download files
            $transferResult = $session.GetFiles($session.EscapeFileMask($source + "/" + $readyFile.Name), $destination, $True, $transferOptions)
            
            # Throw on any download error
            $transferResult.Check()
     
            # Log download results
            foreach ($transfer in $transferResult.Transfers)
            {
                LogWrite ("{0}: Download of {1} succeeded" -f (Get-Date), $transfer.FileName)
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    LogWrite ("{0}: {1}" -f (Get-Date),  $_.Exception.Message)
    exit 1
}
Logs:
attached

winscp get script logs.PNG

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,286
Location:
Prague, Czechia

Re: Sporadic error code 2 while getting files from SFTP server

I can only guess that the file is removed between your script retrieves the list of files to transfer and the time it gets to actual transfer the file. Verify that. Or post a session log file. If it turns out to be the case, you will have to handle that more gracefully – by not aborting the script – what you are currently doing by calling $transferResult.Check(). Instead, test the $transferResult.IsSuccess. See for example Recursively download directory tree with custom error handling

Reply with quote

Arcplace
Guest

Hey Martin
Many thanks for your feedback. Indeed the files are beeing moved in the same time this script is trying to dowload. Unfortunately the script has been initiated as well on the test environment with the wrong parameters. However, I will update the script and optimize it according to your recommendation.

Best Regards!
MN

Reply with quote

Advertisement

You can post new topics in this forum