Missing files after transfer from local to remote using PowerShell script

Advertisement

saymoncoppi
Joined:
Posts:
1
Location:
BR

Missing files after transfer from local to remote using PowerShell script

Hello everyone!
We are facing a situation with one transfer files scenario. Describing a little...
My app writes files in the folder to be sent to the customer FTP.
So based on this we moved from bat script to sample PowerShell below:
https://winscp.net/eng/docs/script_local_move_after_successful_upload

It works 99%... few times customer inquire me that some files were not received there. I think it's impossible since we only move files to a backup folder when there's no error on transmission.

I made some logs to catch exceptions or issues. But I cannot see anything wrong.
Could someone help me to discover what is happening? Or how to answer back to the customer showing him that maybe the issue could be there on his side losing the file when they process it.

See my script:
param (
    $localPath = "F:\sap-files\production\out\*",
    $remotePath = "/RFID/OUT/",
    $backupPath = "F:\sap-files\production\out_arch\",
    $log_file = "F:\sap-files\production\log.txt"
)
 
try {
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Ftp
        HostName = "ftp_ip"
        UserName = "ftp_user"
        Password = "ftp_password"
    }
 
    $session = New-Object WinSCP.Session
 
    try {
        # Connect
        $session.Open($sessionOptions)
 
        # Upload files, collect results
        $transferResult = $session.PutFiles($localPath, $remotePath)
 
        # Iterate over every transfer
        foreach ($transfer in $transferResult.Transfers) {
            # Success or error?
            if ($transfer.Error -eq $Null) {
                
                $msg_success_upload = "$(Get-Date) - Upload of $($transfer.FileName) succeeded!"
                Write-Host $msg_success_upload
                # Add to log
                Add-Content -Path $log_file -Value $msg_success_upload
                
                # Upload succeeded, move source file to backup
                $msg_success_move_to_backup = "$(Get-Date) - Backup of $($transfer.FileName) succeeded!"
                Write-Host $msg_success_move_to_backup   
                Move-Item $transfer.FileName $backupPath -force
                # Add to log
                Add-Content -Path $log_file -Value $msg_success_move_to_backup
            }
            else {
                $msg_error_upload = "$(Get-Date) - Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
                # Add to log
                Add-Content -Path $log_file -Value $msg_error_upload
            }
        }
    }
    catch {
        $msg_ftp_not_connect = "$(Get-Date) - Connection error!"
        Write-Host $msg_ftp_not_connect
        # Add to log
        Add-Content -Path $log_file -Value $msg_ftp_not_connect
    }
    finally {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch {
    $msg_except_error = "Error: $($_.Exception.Message)"
    Write-Host $msg_except_error
    # Add to log
    Add-Content -Path $log_file -Value $msg_except_error
    exit 1
}

Reply with quote

Advertisement

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

Re: Missing files after transfer from local to remote using PowerShell script

You have the logs. You can show them that the specific file that they claim to be missing was indeed uploaded to their server.

Reply with quote

Advertisement

You can post new topics in this forum