Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

gopihc

Re: 'PreserveTimestamp = $false' not working -- PowerShell script

@martin: This worked.
coffeecoffeecoffee

Awesome thanks @Martin. That worked great! I missed that documentation.
coffeecoffeecoffee

[SOLVED] 'PreserveTimestamp = $false' not working -- PowerShell script

I am setting up a new job and the host is a bit more restrictive than others I've worked with. I'm continually running into the error below even though I've added the setting to not PreserveTimestamp.

Error: Exception calling "Check" with "0" argument(s): "**Upload of file 'somefile.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.
The server does not support the operation.
Error code: 8
Error message from server (US-ASCII): SETSTAT unsupported"

The script is basic:
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "c:\TEST\WinSCP\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "somehost.somedomain.com"
        UserName = "username"
        Password = "password"
        GiveUpSecurityAndAcceptAnySshHostKey = $True
    }
 
    $session = New-Object WinSCP.Session
    $session.SessionLogPath = "C:\TEST\WinSCP\Logs\winscp.log"
 
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.FilePermissions = $Null # This is default
    $transferOptions.PreserveTimestamp = $false
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
        $remotePath = "./"
 
        $localpath = "\\networkpath\sharename\directory\"
        $files = @(
            ("somefile.txt"),
            ("somefile2.txt"),
            ("somefile3.txt"),
            ("somefile4.txt"),
            ("somefile5.txt")
        )
 
        foreach ($file in $files)
        {
            Write-Host "Uploading $file ..."
            $session.PutFiles($localpath + $file, $remotePath, $transferOptions).Check()
        }
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
}

Oddly, it appears that the transfer option to not preserve timestamp is being ignored, according to the log:
> DATE TIME Script: put -delete -nopermissions -preservetime -transfer="binary" -- "\\networkpath\sharename\directory\somefile.txt" "./"

I can provide additional log detail if necessary.

I have also tried using these transfer options:
$transferOptions.AddRawSettings("IgnorePermErrors", "1")
$transferOptions.AddRawSettings("PreserveTimeDirs", "0")

Version:
WinSCP Version 5.17.8 (Build 10803) (OS 10.0.17763 - Windows Server 2019 Standard)