1

I'm getting this error:

The property 'PreserveTimestamp' cannot be found on this object. Verify that the property exists and can be set.
At path\OSBU_Broker_Extract02.ps1:14 
char:3
+   $session.PreserveTimestamp = $False
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
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.

I'm not sure where I should set my variable, please see my code below:

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "[server IP]"
    UserName = "[username]"
    Password = ""
    SshHostKeyFingerprint = "ssh-rsa 1024 ...="
}

$session = New-Object WinSCP.Session
$session.PreserveTimestamp = $False

try
{
    # Connect
    $session.Open($sessionOptions)

    # Upload
    $session.PutFiles("filename.xlsx", "/destination/").Check()
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992

1 Answers1

0

The PreserveTimestamp is a property of the TransferOptions class, not Session.

A TransferOptions instance should be passed as a fourth argument of the Session.PutFiles:

# Set up transfer options
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
    PreserveTimestamp = $False
}

# Transfer files
$session.PutFiles("filename.xlsx", "/destination/", $False, $transferOptions).Check()

Also, in general, the paths should be absolute (not just filename.xlsx).

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992