I adopted this script from the forum for use with directory synchronization of my FTP site. It works well except that the script will quit if one of the files fails to download. It appeared to be prompting a user input and then retried a few times. Afterward, it disconnected instead of moving on to the next file.
What I expect the script to do is to continue the synchronization by skipping to the next file in a situation like this. I read about "option batch abort" and "option confirm off" but do not know how I can apply it to the script. I am not sure if that would work too. Can anyone please help?
Log entries and the script I used are attached below.
========== Synchronization log ==================================
. 2015-06-28 09:47:43.046 Can't open file 'D:\Clients\ICON'.
. 2015-06-28 09:47:43.046 System Error. Code: 5.
. 2015-06-28 09:47:43.046 Access is denied
. 2015-06-28 09:47:43.046 Copying files from remote side failed.
* 2015-06-28 09:47:43.046 (ExtException) **Can't open file 'D:\Clients\ICON'.
* 2015-06-28 09:47:43.046 System Error. Code: 5.
* 2015-06-28 09:47:43.046 Access is denied**
* 2015-06-28 09:47:43.046 Copying files from remote side failed.
. 2015-06-28 09:47:43.062 Asking user:
. 2015-06-28 09:47:43.062 Error transferring file '/ICON'. ("Can't open file 'D:\Clients\ICON'.
. 2015-06-28 09:47:43.062 System Error. Code: 5.
. 2015-06-28 09:47:43.062 Access is denied","Copying files from remote side failed.")
< 2015-06-28 09:47:43.062 Script: Error transferring file '/ICON?'.
< 2015-06-28 09:47:43.062 Script: Can't open file 'D:\Clients\ICON?'.
< 2015-06-28 09:47:43.062 System Error. Code: 5.
< 2015-06-28 09:47:43.062 Access is denied
< 2015-06-28 09:47:43.062 Copying files from remote side failed.
* 2015-06-28 09:47:43.062 (EScpSkipFile) Error transferring file '/ICON'.
* 2015-06-28 09:47:43.062 Can't open file 'D:\Clients\ICON'.
* 2015-06-28 09:47:43.062 System Error. Code: 5.
* 2015-06-28 09:47:43.062 Access is denied
* 2015-06-28 09:47:43.062 Copying files from remote side failed.
. 2015-06-28 09:47:43.077 Script: Failed
> 2015-06-28 09:47:43.343 Script: exit
. 2015-06-28 09:47:43.343 Script: Exit code: 1
. 2015-06-28 09:47:43.343 Disconnected from server
==============================================================================
# ============ Synchronization Script ==========================================
param (
$localPath = "C:\upload\*",
$remotePath = "/home/user/",
$backupPath = "C:\backup\"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "mypassword"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
$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-Host ("Upload of {0} succeeded, moving to backup" -f
$transfer.FileName)
# Upload succeeded, move source file to backup
Move-Item $transfer.FileName $backupPath
}
else
{
Write-Host ("Upload of {0} failed: {1}" -f
$transfer.FileName, $transfer.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
===============================================================================