Powershell to copy multiple files to SFTP server

Advertisement

Userp
Joined:
Posts:
1
Location:
United States

Powershell to copy multiple files to SFTP server

I am using the sample script found here at WinSCP to upload files in a local directory to a directory on an SFTP server, then move the local file. The Powershell script works, but only uploads one file to the SFTP directory at a time. I have to run the script multiple times to move all files. Not sure why that is. This is the script.

param (
    $localPath = "c:\ebanking\PosPay\*.*" ,
    $RemotePath = "/incoming/arp/" ,
    $backupPath = "C:\eBanking\Z_Arch\PosPay"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
 
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "fdqn.computer.com"
    UserName = "xxxxxxx"
    Password = "xxxxxxxxxxx"
    SshHostKeyFingerprint = "ssh-rsa 1024 abcdefghijklmnopqrstuvwxyz"
    
}
 
$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)
            {
                Write-Output "Upload of $($transfer.FileName) succeeded, moving to backup" >> C:\eBanking\Z_Logs\BofAPP.log
                # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath
            }
            else
            {
                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
            }
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}

Any ideas why not all the files on local directory are not moved?

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
28,287
Location:
Prague, Czechia

Re: Powershell to copy multiple files to SFTP server

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

Advertisement

You can post new topics in this forum