my Script works very nice but now i would like to add an filemask to transfer only files older than 5minutes.
param (
$localPath = "F:\Test-Sync01\",
$remotePath = "/Test-Sync01/"
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "F:\syncapp\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "server08"
$sessionOptions.UserName = "syncusr"
$sessionOptions.Password = "BLBALBLA"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 13:d3:ef:ee:4d:cc:22:31:04:aa:1e:cd:7b:c7:42:02"
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*<5N"
# Synchronize files to local directory, collect results
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Remote, $localPath, $remotePath, $False, transferOptions)
# Iterate over every download
foreach ($download in $synchronizationResult.Uploads)
{
echo $download.FileName
# Success or error?
if ($download.Error -eq $Null)
{
Write-Host ("Download of {0} succeeded, removing from source" -f
$download.FileName)
try
{
Remove-Item $session.EscapeFileMask($download.FileName)
Write-Host ("Removing of file {0} succeeded" -f
$download.FileName)
}
catch [Exception]
{
Write-Host ("Rmoving of file {0} failed" -f
$download.FileName)
}
}
else
{
Write-Host ("Download of {0} failed: {1}" -f
$download.FileName, $download.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}