I am using WinSCP .NET Assembly in PowerShell for file synchronization with a SFTP server. I am using the following code for synchronization:
http://winscp.net/eng/docs/library_session_synchronizedirectories#powershell
Problem is, when this script sync files through upload to SFTP it generates error, although it actually uploads the file. The script gets terminated immediately with the following error:
Upload of C:\FileSync\files\test2.txt succeeded
Permissions of /Reports/test2.txt kept with their defaults
Setting timestamp of /Reports/test2.txt failed:
WinSCP.SessionRemoteException: Upload of file 'test2.txt' was successful, but error occurred while setting the permissions and/or timestamp.
If the problem persists, turn off setting permissions or preserving timestamp. Alternatively you can turn on 'Ignore permission errors' option.
---> WinSCP.SessionRemoteException: The server does not support the operation.
Error code: 8
Error message from server: SSHServerAPI.SFTP.fxp_attrs
--- End of inner exception stack trace ---
I am not finding any way how to "ignore permission error" as it's suggested in the errors.
The script does not complain when doing the syncchroniation through downloading files from SFTP.
Any help please?
The error is documented here:
https://winscp.net/eng/docs/message_preserve_time_perm
I assume you did not enable setting permissions (it's off by default). If you did, turn it off by setting the TransferOptions.FilePermissions
(see below).
Your server probably does not support updating timestamps of remote file. That makes it complicated to allow local-to-remote synchronization of files against such server as the timestamps are primary criteria to compare the files.
In general it does not make sense to turn off updating timestamp with synchronization as the update is basically an integral part of the synchronization.
The hint to "Ignore permission errors" is there for basic file transfers. As mentioned already, it does not make sense for synchronization.
So all you can to is to:
turn off updating the timestamps (set TransferOptions.PreserveTimestamp
);
and make WinSCP not consider the timestamps, when comparing files (set criteria
parameter of Session.SynchronizeDirectories
to [WinSCP.SynchronizationCriteria]::Size
or None
).
It's questionable though how such synchronization is meaningful.
$transferOptions = New-Object WinSCP.TransferOptions
...
$transferOptions.FilePermissions = $Null # This is default
$transferOptions.PreserveTimestamp = $False
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Remote, "d:\www", "/home/martin/public_html",
$False, $False, [WinSCP.SynchronizationCriteria]::Size)