Differences
This shows you the differences between the selected revisions of the page.
2016-03-07 | 2016-03-07 | ||
correcting method name and syntax highlighting (martin) | simplifying PowerShell example (martin) | ||
Line 174: | Line 174: | ||
$session.Open($sessionOptions) | $session.Open($sessionOptions) | ||
- | $stamp = Get-Date -f "yyyyMMdd" | + | # Download files |
- | $fileName = "export_$stamp.txt" | + | ········$transferOptions = New-Object WinSCP.TransferOptions |
- | $remotePath = "/home/user/sysbatch/$fileName" | + | $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary |
- | $localPath = "d:\backup\$fileName" | + | · |
- | + | $transferResult = $session.GetFiles("/home/user/*", "d:\download\", $False, $transferOptions) | |
- | ·······# Manual "remote to local" synchronization. | + | |
- | + | # Throw on any error | |
- | # You can achieve the same using: | + | $transferResult.Check() |
- | # $transferOptions = New-Object WinSCP.TransferOptions | + | |
- | # $transferOptions.FileMask = $fileName | + | # Print results |
- | # $session.SynchronizeDirectories( | + | foreach ($transfer in $transferResult.Transfers) |
- | # [WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, | + | |
- | # $False, $False, [WinSCP.SynchronizationCriteria]::Time, | + | |
- | # ····$transferOptions).Check() | + | |
- | if ($session.FileExists($remotePath)) | + | |
{ | { | ||
- | if (!(Test-Path $localPath)) | + | Write-Host ("Download of {0} succeeded" -f $transfer.FileName) |
- | { | + | |
- | ················Write-Host ( | + | |
- | ····················"File {0} exists, local backup {1} does not" -f | + | |
- | $remotePath, $localPath) | + | |
- | $download = $True | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | $remoteWriteTime = $session.GetFileInfo($remotePath).LastWriteTime | + | |
- | $localWriteTime = (Get-Item $localPath).LastWriteTime | + | |
- | + | ||
- | if ($remoteWriteTime -gt $localWriteTime) | + | |
- | { | + | |
- | Write-Host ( | + | |
- | ("File {0} as well as local backup {1} exist, " + | + | |
- | "but remote file is newer ({2}) than local backup ({3})") -f | + | |
- | ·······················$remotePath, $localPath, $remoteWriteTime, $localWriteTime) | + | |
- | $download = $True | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | Write-Host ( | + | |
- | ("File {0} as well as local backup {1} exist, " + | + | |
- | "but remote file is not newer ({2}) than local backup ({3})") -f | + | |
- | $remotePath, $localPath, $remoteWriteTime, $localWriteTime) | + | |
- | $download = $False | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | if ($download) | + | |
- | { | + | |
- | # Download the file and throw on any error | + | |
- | $session.GetFiles($remotePath, $localPath).Check() | + | |
- | + | ||
- | Write-Host "Download to backup done." | + | |
- | } | + | |
- | } | + | |
- | else | + | |
- | { | + | |
- | Write-Host ("File {0} does not exist yet" -f $remotePath) | + | |
} | } | ||
} | } |