Re: 'PreserveTimestamp = $false' not working -- PowerShell script
@martin: This worked.
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"
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)"
}
> DATE TIME Script: put -delete -nopermissions -preservetime -transfer="binary" -- "\\networkpath\sharename\directory\somefile.txt" "./"
$transferOptions.AddRawSettings("IgnorePermErrors", "1")
$transferOptions.AddRawSettings("PreserveTimeDirs", "0")
WinSCP Version 5.17.8 (Build 10803) (OS 10.0.17763 - Windows Server 2019 Standard)