0
votes

I am trying to add -nopreservetime to this code. I am not sure where or how to add it, I have tried different things to no result.

# Setup session options 
$sessionOptions = New-Object WinSCP.SessionOptions 
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp 
$sessionOptions.HostName = "#######.com" 
$sessionOptions.UserName = "#####" 
$sessionOptions.Password = "#####" 
$sessionOptions.SshHostKeyFingerprint = "#####" 
$sessionOptions.Timeout = "##" 

$TransferOptions.PreserveTimestamp = $false 

$session = New-Object WinSCP.Session 
$Session.SessionLogPath = "C:\Users\user1\Desktop\Winscp\TEST\sessionlog.txt" 
$Session.debugLogPath = "C:\Users\user1\Desktop\Winscp\TEST\debugsessionlog"+$date+".txt"

try 
{ 
    Write-Host $date,("Connecting to FTP site.") 
    # Connect 
    $session.Open($sessionOptions) 
    Write-Host $date,("Connected to FTP site.") 

    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary 

    # Upload files, collect results 
    $transferResult = $session.PutFiles("$localPath", "$remotePath", $False, $transferOptions)
}

This is normally:

$transferResult = $session.PutFiles($localPath, $remotePath) 

I am just trying to disable preserve timestamp. I have it off in the GUI but it isn't working in the powershell_ise.exe

The above code returns this in the log:

2015-07-20 08:52:44.635 Script: put -nopermissions -preservetime -transfer="binary" -- "C:\Users\user\Desktop\Winscp############*.pdf" "/OUTGOING/TEST#####/##/"

1

1 Answers

0
votes

Use the TransferOptions.PreserveTimestamp:

$transferOptions.PreserveTimestamp = $False

See Converting transfer settings scripting switches to code based on .NET assembly.


You actually already have that piece of code there, but in a wrong place. You set a property of an uninitialized variable.

You have to move the line after the initialization of the $transferOptions:

$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary 
$transferOptions.PreserveTimestamp = $False