Differences
This shows you the differences between the selected revisions of the page.
| 2020-12-21 | 2020-12-21 | ||
| Simplified for 5.18.1 with use of .Reverse() and (implied) .ToString() (martin) | whitespaces (martin) | ||
| Line 31: | Line 31: | ||
| # @option SessionLogPath -config sessionlogfile | # @option SessionLogPath -config sessionlogfile | ||
| # @optionspage ~~SELF~~#options | # @optionspage ~~SELF~~#options | ||
| - | · | + | |
| param ( | param ( | ||
| # Use Generate Session URL function to obtain a value for -sessionUrl parameter. | # Use Generate Session URL function to obtain a value for -sessionUrl parameter. | ||
| Line 47: | Line 47: | ||
| $refresh | $refresh | ||
| ) | ) | ||
| - | · | + | |
| try | try | ||
| { | { | ||
| Line 57: | Line 57: | ||
| $sessionOptions = New-Object WinSCP.SessionOptions | $sessionOptions = New-Object WinSCP.SessionOptions | ||
| $sessionOptions.ParseUrl($sessionUrl) | $sessionOptions.ParseUrl($sessionUrl) | ||
| - | · | + | |
| $listPath = [Environment]::ExpandEnvironmentVariables($listPath) | $listPath = [Environment]::ExpandEnvironmentVariables($listPath) | ||
| $listDir = (Split-Path -Parent $listPath) | $listDir = (Split-Path -Parent $listPath) | ||
| New-Item -ItemType directory -Path $listDir -Force | Out-Null | New-Item -ItemType directory -Path $listDir -Force | Out-Null | ||
| - | · | + | |
| if (Test-Path $listPath) | if (Test-Path $listPath) | ||
| { | { | ||
| Line 72: | Line 72: | ||
| $previousFiles = @() | $previousFiles = @() | ||
| } | } | ||
| - | · | + | |
| $needRefresh = $False | $needRefresh = $False | ||
| $session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
| - | ···· | + | |
| try | try | ||
| { | { | ||
| $session.SessionLogPath = $sessionLogPath | $session.SessionLogPath = $sessionLogPath | ||
| - | · | + | |
| Write-Host "Connecting..." | Write-Host "Connecting..." | ||
| $session.Open($sessionOptions) | $session.Open($sessionOptions) | ||
| - | · | + | |
| Write-Host "Comparing files..." | Write-Host "Comparing files..." | ||
| $differences = | $differences = | ||
| $session.CompareDirectories( | $session.CompareDirectories( | ||
| [WinSCP.SynchronizationMode]::Both, $localPath, $remotePath, $False) | [WinSCP.SynchronizationMode]::Both, $localPath, $remotePath, $False) | ||
| - | · | + | |
| if ($differences.Count -eq 0) | if ($differences.Count -eq 0) | ||
| { | { | ||
| Line 96: | Line 96: | ||
| { | { | ||
| Write-Host "Synchronizing $($differences.Count) change(s)..." | Write-Host "Synchronizing $($differences.Count) change(s)..." | ||
| - | · | + | |
| foreach ($difference in $differences) | foreach ($difference in $differences) | ||
| { | { | ||
| Line 157: | Line 157: | ||
| $session.Dispose() | $session.Dispose() | ||
| } | } | ||
| - | · | + | |
| # Refresh the remote directory in WinSCP GUI, if it was changed and -refresh switch was used | # Refresh the remote directory in WinSCP GUI, if it was changed and -refresh switch was used | ||
| if ($refresh -and $needRefresh) | if ($refresh -and $needRefresh) | ||
| Line 164: | Line 164: | ||
| & "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath" | & "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath" | ||
| } | } | ||
| - | · | + | |
| Write-Host "Saving current local file list..." | Write-Host "Saving current local file list..." | ||
| $localFiles = | $localFiles = | ||
| Line 170: | Line 170: | ||
| Select-Object -ExpandProperty FullName | Select-Object -ExpandProperty FullName | ||
| Set-Content $listPath $localFiles | Set-Content $listPath $localFiles | ||
| - | · | + | |
| Write-Host "Done." | Write-Host "Done." | ||
| - | ···· | + | |
| $result = 0 | $result = 0 | ||
| } | } | ||
| Line 180: | Line 180: | ||
| $result = 1 | $result = 1 | ||
| } | } | ||
| - | · | + | |
| # Pause if -pause switch was used | # Pause if -pause switch was used | ||
| if ($pause) | if ($pause) | ||
| Line 187: | Line 187: | ||
| [System.Console]::ReadKey() | Out-Null | [System.Console]::ReadKey() | Out-Null | ||
| } | } | ||
| - | · | + | |
| exit $result | exit $result | ||
| </code> | </code> | ||