-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiPathRemoteToLocalSync.ps1
271 lines (203 loc) · 10.7 KB
/
MultiPathRemoteToLocalSync.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<#
.SYNOPSIS
WinSCP - MultiPath Remote to Local Sync
2.0.0
.DESCRIPTION
Originally based off the functionality of KeepLocalUpToDate.WinSCPextension.ps1 script which is provided in the
extension folder of WinSCP 5.9.2+. To improve the ability of the closed functionality script, rewrote the enitire thing in
powershell, besides the last part, syncing, which will eventually be rewriten as well. At this point it will take input of
multiple folders, local and remote, that are accepted as pairs. Each pair will sync between and allow the contents of the
remote to be sync'd to the local. There is a interval that allows the script to continuously run and have a delay between
session of checking the folders provided. It will also disconnect and reconnect between each session, to ensure the
connection maintains and does not drop at any point. This still uses the
.PARAMETER multiplePaths
Input for multiple local and remote paths. Each pair needs a local and remote path which will have the local listed
first with the remote path immediately after.
(i.e. "B:\Backup\SYS\","/SYS/","B:\Backup\PROC\","/PROC/","B:\Backup\Docs\","/Users/TestUser/Documents/")
.PARAMETER sessionProtocol
Protocol to be used to connect with. (i.e. FTP, SFTP, SCP, HTTP, HTTPS)
.PARAMETER sessionHostName
HostName of the remote server. (i.e. ftp.testserver.com)
.PARAMETER sessionUserName
UserName of the account on the remote server.
.PARAMETER sessionPassword
Password of the account on the remote server.
.PARAMETER sessionSSHHostKeyFingerprint
SSH Host Key of the remote server, used in along with secure connections, SFTP or SCP
.PARAMETER localPath
Local path if only one path is going to be synchronized.
.PARAMETER remotePath
Remote path of only one path is going to be synchronized.
.PARAMETER interval
Amount in seconds to pause between synchronization loops, that allows the script to be cancelled with Ctrl-C.
.EXAMPLE
./MultiPathRemoteToLocalSync.ps1 -multiplePaths "B:\Backup\SYS\","/SYS/","B:\Backup\PROC\","/PROC/" -sessionProtocol ftp
-sessionHostName ftp.testserver.com -sessionUserName TestUser -sessionPassword TestPASSWORD
./MultiPathRemoteToLocalSync.ps1 -sessionProtocol ftp -sessionHostName ftp.testserver.com -sessionUserName TestUser
./MultiPathRemoteToLocalSync.ps1 -verbose
.NOTES
With the initial version of this script it setup for 3 remote/local folders that are manually set through parameters. The
original WinSCP script and documentation - https://winscp.net/eng/docs/library_example_keep_local_directory_up_to_date
.LINK
https://github.com/delta911turbo/MultiPathRemoteToLocalSync
#>
[cmdletbinding()]
Param(
[Parameter(ValueFromPipeline=$True)]
[String[]] $multiplePaths, ## Multiple path input format: "localpath1","remotepath1","localpath2","remotepath2" ##
[Parameter(Mandatory = $True, ValueFromPipeline=$true)]
[ValidateScript({ (($PsItem -eq "ftp") -or ($PsItem -eq "sftp") -or ($PsItem -eq "scp") -or ($PsItem -eq "webdev")) })] ## Verifies input is a valid protocol ##
$sessionProtocol, ## Valid protocols: ftp, sftp, scp, http, https ##
[Parameter(Mandatory = $True, ValueFromPipeline=$true)]
[ValidateScript({ (($PsItem -ne $null) -and ($PsItem -ne [String]::Empty)) })] ## Verifies input was not NULL ##
$sessionHostName,
[Parameter(Mandatory = $True, ValueFromPipeline=$true)]
[ValidateScript({ (($PsItem -ne $null) -and ($PsItem -ne [String]::Empty)) })] ## Verifies input was not NULL ##
$sessionUserName,
[Parameter(Mandatory = $True, ValueFromPipeline=$true)]
$sessionPassword,
[parameter(Mandatory=$false, ValueFromPipeline=$true)]
$sessionSSHHostKeyFingerprint,
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
$localPath,
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
$remotePath,
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
$interval = 300
)
Write-Verbose "################## Starting Variable Values ##################"
Write-Verbose "Multipath: $multiplePaths"
Write-Verbose "Protocol: $sessionProtocol"
Write-Verbose "HostName: $sessionHostName"
Write-Verbose "Username: $sessionUserName"
Write-Verbose "Password: $sessionPassword"
Write-Verbose "Localpath: $localPath"
Write-Verbose "Remotepath: $remotePath"
Write-Verbose "SSHHostKeyFingerprint: $sessionSSHHostKeyFingerprint"
function DataValidation {
## Testing steps and data flow ##
Write-Verbose "################## Data Validation Values ##################"
Write-Verbose "Multipath: $multiplePaths"
Write-Verbose "Protocol: $sessionProtocol"
Write-Verbose "HostName: $sessionHostName"
Write-Verbose "Username: $sessionUserName"
Write-Verbose "Password: $sessionPassword"
Write-Verbose "Localpath: $localPath"
Write-Verbose "Remotepath: $remotePath"
Write-Verbose "SSHHostKeyFingerprint: $sessionSSHHostKeyFingerprint `n"
## Checks for secure protocols, SFTP and SCP, and verifies/aquires SSH Host Key Fingerprint ##
if ((!$sessionSSHHostKeyFingerprint) -and ($sessionProtocol -eq "sftp") -or ($sessionProtocol -eq "scp")) {
$sessionSSHHostKeyFingerprint = Read-host "SSH HostKey Fingerprint: "
## Verifies SSH Host Key Fingerprint was provided ##
if (!$sessionSSHHostKeyFingerprint) {throw "SSH Host Key Fingerprint is required for secure protocols, SFTP and SCP. Please visit https://winscp.net/eng/docs/faq_hostkey for more information."}
}
## Check if $multiplePaths variable contains data and verifies that the local paths are valid ##
if ($multiplePaths) {
$i = 0
$pathSet = 1
do {
if (!$multiplePaths[$i]) {throw "Missing Local Path $pathSet"} ## Verifies that local path is not NULL ##
if (Test-path $multiplePaths[$i]) {} else {throw "Local Path $pathSet is invalid"} ## Test local path is valid ##
write-verbose "############ Local Path $pathSet ###############"
write-verbose $multiplePaths[$i]
$i++
if (!$multiplePaths[$i]) {throw "Missing Remote Path $pathSet"} ## Verifies that remote path is not NULL ##
write-verbose "############ Remote Path $pathSet ###############"
write-verbose $multiplePaths[$i]
$i++
$pathSet++
} while ($i -lt $multiplePaths.count)
} else {
## Request user input for local path if one is not provided as a parameter when the script is called ##
if (!$localPath) { $localPath = Read-host "Local path: " }
## Requet user input for remote path if one is not provided as a parameter when the script is called ##
if (!$remotePath) { $remotePath = Read-host "Remote path: " }
}
return
}
function WinSCPSync {
Param (
[string] $multiLocalPath,
[string] $multiRemotePath
)
if ($multiLocalPath) { $localPath = $multiLocalPath }
if ($multiRemotePath) { $remotePath = $multiRemotePath }
Write-Verbose "################## Sync Values ##################"
Write-Verbose "Multipath: $multiplePaths"
Write-Verbose "Protocol: $sessionProtocol"
Write-Verbose "HostName: $sessionHostName"
Write-Verbose "Username: $sessionUserName"
Write-Verbose "Password: $sessionPassword"
Write-Verbose "Localpath: $localPath"
Write-Verbose "Remotepath: $remotePath"
Write-Verbose "SSHHostKeyFingerprint: $sessionSSHHostKeyFingerprint"
## Generating list of files from the local path ##
$localFiles = Get-ChildItem $localPath | Select -Property Name
## Verbose printout of Local Files found ##
Write-Verbose "################## Local Files in $localPath #####################"
$localFiles | ForEach-Object { Write-Verbose $_.Name}
## Generating list of files from remote path ##
$remoteFiles = $session.ListDirectory($remotePath).Files | Select -Property Name
## Verbose printout of Remote Files found ##
Write-Verbose "################## Remote Files in $remotePath ####################"
$remoteFiles | Where-Object {$_.Name -ne ".."} | ForEach-Object {Write-Verbose $_.Name}
Write-host "###################### Comparing folders $remotePath and $localPath ##################"
## Comparing files found in local path to files found in remote path ##
Compare-Object -ReferenceObject $remoteFiles -Property Name -DifferenceObject @($localFiles | Select-Object) | Where-Object { $_.SideIndicator -eq "<=" -and $_.Name -ne ".." } | Foreach-Object {
## Syncing any missing files from local path ##
Write-Host "Syncing new file $remotePath$($_.Name)"
$session.GetFiles($session.EscapeFileMask($remotePath + $($_.Name)), $localPath).check()
}
}
DataValidation
Add-Type -Path "..\WinSCPnet.dll"
# PowerShell While Loop
$i =9
While ($i -gt 8) {
if ($sessionSSHHostKeyFingerprint) {
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::$sessionProtocol
HostName = $sessionHostName
UserName = $sessionUserName
Password = $sessionPassword
SSHHostKeyFingerprint = $sessionSSHHostKeyFingerprint
}
} else {
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::$sessionProtocol
HostName = $sessionHostName
UserName = $sessionUserName
Password = $sessionPassword
}
}
$session = New-Object WinSCP.Session
## Connecting to remote server ##
Write-Host "############### Connecting... ###############"
$session.Open($sessionOptions)
if (!$multiplePaths) { WinSCPSync } else {
$localIndex = 0
$remoteIndex =1
do {
if (!$multiplePaths[$localIndex]) {throw "Missing Local Path $localIndex"}
$localPath = $multiplePaths[$localIndex]
if (!$multiplePaths[$remoteIndex]) {throw "Missing Remote Path $remoteIndex"} ## Verifies that remote path is not NULL ##
$remotePath = $multiplePaths[$remoteIndex]
WinSCPSync
$localIndex++
$localIndex++
$remoteIndex++
$remoteIndex++
} while ($remoteIndex -lt $multiplePaths.count)
}
write-host "############## Disconnecting... ##############"
# Disconnect, clean up
$session.Dispose()
Write-Host "Waiting for $interval seconds, press Ctrl+C to abort..."
$wait = [int]$interval
# Wait for 1 second in a loop, to make the waiting breakable
while ($wait -gt 0)
{
Start-Sleep -Seconds 1
$wait--
}
}