Search code examples
linuxpowershellsftpwinscpwinscp-net

"Upload of file '...' was successful, but error occurred while setting the permissions and/or timestamp" when using WinSCP .NET assembly in PowerShell


Exception calling "Check" with "0" argument(s): "Upload of file '2019-06-11.zip'
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.
Permission denied.
Error code: 3
Error message from server: This server does not support operations to modify
file attributes."
At line:12 char:84
+     $session.PutFiles("D:\Users\bin\*.zip", "/Outbox/").Check <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I keep on getting above error file transferring file from Window Server to Linux. I got the same error when using the WinSCP GUI as well. I asked MFT team and they didn't any set permission. Below are my script for file transferring and some of intro version of software I'm using. Anything I missed out for my script or version of software is too old? I will have an update of server soon but have to wait another 2 yrs. This task will be set as scheduler to transfer file daily to MFT server.

Version of software:

  1. Use .NET 4.0
  2. Use PowerShell v2.0
  3. Window Server 2008
  4. Placed private.ppk, WinSCPNet.dll and WinSCP.exe at same folder
#Load WinSCP .NET assembly
Add-Type -Path "D:\Users\WinSCPnet.dll" -Verbose

$session = New-Object WinSCP.Session
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "[Linux server IP]"
$sessionOptions.UserName = "[username]"
$sessionOptions.PortNumber = "[linux port number]"
$sessionOptions.Password = ""
$sessionOptions.SshPrivateKeyPath = "D:\Users\bin.ppk"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 ....="

try {
    # Open the WinSCP.Session object using the WinSCP.SessionOptions object.
    $session.Open($sessionOptions)

    # Upload
    $session.PutFiles("D:\Users\bin\*.zip", "/Outbox/").Check()
} finally {
    # Disconnect, clean up
    $session.Dispose()
}

Solution

  • The error is documented here:
    https://winscp.net/eng/docs/message_preserve_time_perm

    Your server does not support updating timestamps of uploaded remote files. So you need to instruct WinSCP not to attempt it:

    $transferOptions = New-Object WinSCP.TransferOptions
    ...
    $transferOptions.PreserveTimestamp = $False
    
    $session.PutFiles("D:\Users\bin\*.zip", "/Outbox/", $False, $transferOptions).Check()