Trying to run the following script. Connect to a ftp / sftp server and get the files from a remotepath to a localpath
Since there are a list of sources and destinations, I decided to create a csv file and pass the value using $.Source and $.Destination
Session.getfiles () seems to not be working when I import my csv file. But if I hard code the path, it seems to work.
$session.GetFiles("c:\source\test", "c:\destination\", $False, $transferOptions)
Goal: Script reads the list of Sources and move files according to its destination. Also go back to a # amount of days and downdload the latest file -
Error: "No such File" or "cant get attributes of file"
try{
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::sftp
HostName = "xxxxx"
UserName = "xxxxr"
Password = "xxxxxxx"
PortNumber="xx"
FTPMode="Active"
GiveUpSecurityAndAcceptAnySshHostKey = $true
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
Import-Csv -Path "C:\movefiles.csv" -ErrorAction Stop
$transferResult =
$session.GetFiles($_.Source, $_.Destination, $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Download of $($transfer.FileName) succeeded"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
#exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
#exit 1
}