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:
41,454
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

disfunktedfish
Guest

I am having the same issue described by previous gentlemen with the PowerShell transfer and move script found here:
Moving local files to different location after successful upload

I don't deviate from the script above, I have my SFTP parameters in there and it will transfer one file and move it to the directory. It will only move one. Not sure what I need to do to make sure all of them get moved over.

Here is the log:
Description: Log file from moving one file.

Reply with quote

Advertisement

disfunktedfish
Guest

Solution that worked for me

Hi All,

I found my issue and I'll post here for anyone to use this answer, hopefully at the very least it leads them in the right direction. My original issue is I was only moving one file at a time with this original configuration:
Moving local files to different location after successful upload

The ending solution here, at the end where @Stan.Gobien amends his transfer settings/upload settings is what ended up being the solution for me:
WinSCP .NET assembly (PowerShell) PutFiles from UNC path only uploads 1 file
#transferoptions
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*.*"
$transferOptions.FilePermissions = $Null # This is default
$transferOptions.PreserveTimestamp = $False      
      
# Upload files, collect results
$transferResult = $session.PutFiles(($localPath + "*.*"), ($remotePath + "*.*"), $False, $transferOptions)
I added those options to my script and it worked like a charm. Hopefully everyone else gets help with this down the line if they should have the same issue.

Reply with quote

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

Re: Solution that worked for me

disfunktedfish wrote:

I added those options to my script and it worked like a charm. Hopefully everyone else gets help with this down the line if they should have the same issue.
Thanks for sharing your solution.
Note that only this makes any difference:
$transferOptions.PreserveTimestamp = $False

Reply with quote

Advertisement

You can post new topics in this forum