-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathDownload_file_from_remote_server_via_WinSCP.ps1
45 lines (37 loc) · 1.47 KB
/
Download_file_from_remote_server_via_WinSCP.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
# Download file from SFTP server via WinSCP
# https://winscp.net/eng/docs/library#classes
# C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb
Add-Type -Path "D:\WinSCPnet.dll"
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$Session = New-Object -TypeName WinSCP.Session
$SessionOptions = New-Object -TypeName WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "server"
UserName = "username"
Password = "password1"
PrivateKeyPassphrase = "password2"
SshPrivateKeyPath = "D:\folder\key.ppk"
SshHostKeyFingerprint = "ssh-ed25519 255 xxxxxxxxxxxxxxxx"
}
$Session.Open($sessionOptions)
$TransferOptions = New-Object -TypeName WinSCP.TransferOptions
$TransferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$Directory = $session.ListDirectory("/folder_on_remote_server")
foreach ($file in $Directory.Files)
{
# Get files with current date in their name in "date.month.year" format (17.03.2025)
$File | Where-Object -FilterScript {$_.Name -match (Get-Date).ToString("dd.MM.yyyy")} | ForEach-Object -Process {
# Copy file to folder without deleting the original
$TransferResult = $Session.GetFiles(
$_.FullName,
# Where copy files to
"D:\Download",
# Do not delete original files
$false,
$TransferOptions
)
# Check for errors
$TransferResult.Check()
}
}
$session.Dispose()