在 PowerShell 中使用 WinSCP 仅下载新文件
Download only new files with WinSCP in PowerShell
我怎样才能下载最新的文件,或者发布了特定天数的文件?导入包含 Source 和 Destination 列的 CSV 文件。需要检查路径 exists/file 是否存在并且只下载新文件。
脚本现在正在将所有文件移动到对应的文件夹 - 但是一旦我再次 运行 脚本,它不会只下载新文件。
这是 CSV 文件的示例:
try{
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::sftp
HostName = "xxxxx"
UserName = "xxxxr"
Password = "xxxxxxx"
PortNumber = "xx"
GiveUpSecurityAndAcceptAnySshHostKey = $true
}
$session = New-Object WinSCP.Session
try{
# Connect
$session.Open($sessionOptions)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
Import-Csv -Path "C:\movefiles.csv" -ErrorAction Stop | foreach{
Write-Host $_.Source
Write-host $_.Destination
$transferResult =
$session.GetFiles($_.Source, $_.Destination, $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
$smtpBody = ""
foreach ($transfer in $transferResult.Transfers){
Write-Host "Download of $($transfer.FileName) succeeded"
$smtpbody += " Files : ( $transfer.filename - join ', ')" +
" Current location: $($_.Destination) "
}
Send-Mail Message @smtpMessage -Body $smtpbody
}
finally {
# Disconnect, clean up
$session.Dispose()
}
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
}
}
CSV 文件示例:
Source, Destination
/client1/Business/2019, C:\test\test1
/client2/Reporting/2018, C:\test\test2
如果您的 Source
和 Destination
是纯 目录 路径,您只需将 Session.GetFiles
替换为 Session.SynchronizeDirectories
:
Import-Csv -Path "C:\movefiles.csv" -ErrorAction Stop | foreach {
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $_.Destination, $_.Source, $False)
$synchronizationResult.Check()
foreach ($download in $synchronizationResult.Downloads)
{
Write-Host "File $($download.FileName) downloaded"
}
}
另请参阅 WinSCP 常见问题解答 How do I transfer new/modified files only?
我怎样才能下载最新的文件,或者发布了特定天数的文件?导入包含 Source 和 Destination 列的 CSV 文件。需要检查路径 exists/file 是否存在并且只下载新文件。
脚本现在正在将所有文件移动到对应的文件夹 - 但是一旦我再次 运行 脚本,它不会只下载新文件。
这是 CSV 文件的示例:
try{
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::sftp
HostName = "xxxxx"
UserName = "xxxxr"
Password = "xxxxxxx"
PortNumber = "xx"
GiveUpSecurityAndAcceptAnySshHostKey = $true
}
$session = New-Object WinSCP.Session
try{
# Connect
$session.Open($sessionOptions)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
Import-Csv -Path "C:\movefiles.csv" -ErrorAction Stop | foreach{
Write-Host $_.Source
Write-host $_.Destination
$transferResult =
$session.GetFiles($_.Source, $_.Destination, $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
$smtpBody = ""
foreach ($transfer in $transferResult.Transfers){
Write-Host "Download of $($transfer.FileName) succeeded"
$smtpbody += " Files : ( $transfer.filename - join ', ')" +
" Current location: $($_.Destination) "
}
Send-Mail Message @smtpMessage -Body $smtpbody
}
finally {
# Disconnect, clean up
$session.Dispose()
}
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
}
}
CSV 文件示例:
Source, Destination
/client1/Business/2019, C:\test\test1
/client2/Reporting/2018, C:\test\test2
如果您的 Source
和 Destination
是纯 目录 路径,您只需将 Session.GetFiles
替换为 Session.SynchronizeDirectories
:
Import-Csv -Path "C:\movefiles.csv" -ErrorAction Stop | foreach {
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $_.Destination, $_.Source, $False)
$synchronizationResult.Check()
foreach ($download in $synchronizationResult.Downloads)
{
Write-Host "File $($download.FileName) downloaded"
}
}
另请参阅 WinSCP 常见问题解答 How do I transfer new/modified files only?