I want to download data and then move it on the remote server. I want to keep the folder structure. And exclude the backupfolder for downloading
$localpath = "C:\Temp\Download\"
$remotepath = '//*'
$remotepathbackup = '/backup/'
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Download files, collect results
$transferResult = $session.GetFiles($remotepath, $localpath)
# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host "Download of $($transfer.FileName) succeeded, moving to backup"
# Upload succeeded, move source file to backup
$session.MoveFile(($transfer.FileName), $remotepathbackup)
}
else
{
Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}