$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
FtpMode = [WinSCP.FtpMode]::Passive
...
}
WinSCP GUI can also generate a code example for you:
https://winscp.net/eng/docs/ui_generateurl#code
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
FtpMode = [WinSCP.FtpMode]::Passive
...
}
SetSessionOptions.FtpMode
toFtpMode.Passive
foron
orFtpMode.Active
foroff
.
Enumeration syntax in PowerShell is like[WinSCP.FtpMode]::Passive
.
SessionOptions.FtpMode
:
-passive=on|off
parameter into a WinSCP PowerShell script. This is my code:
#Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $FTPpath
UserName = $user
Password = $pass
SshHostKeyFingerprint = $fingerprint
}
$sessionOptions.AddRawSettings("KEX", "dh-group1-sha1,ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN")
$sessionOptions.AddRawSettings("FSProtocol", "2")
$session = New-Object WinSCP.Session
#Connect
$session.Open($sessionOptions)
#Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$remotePath = Split-Path -Path $FilesToRetrieve -Parent
Write-Host "Remote Path is $remotePath"
# Get list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)
# Select the most recent file
$latest = $directoryInfo.Files |
Where-Object LastWriteTime -gt (Get-Date).AddDays(-$LastWriteTimeDays)
Write-Host "LastWriteTime is $latest"