[英]Pause between file uploads with WinSCP and PowerShell
我有以下 PowerShell 腳本,我非常感謝您對此的任何幫助。
該腳本從網絡驅動器抓取文件名以COKA_*"
開頭的文件,並在何時上傳到 SFTP 站點。
問題:目標站點不喜歡批量接收所有文件,而是一次接收一個。 每次文件傳輸之間有 60 秒的延遲。
在哪里可以在腳本或迭代中添加此延遲以一次只推送一個文件,延遲 60 秒? 我真的很感激你的幫助。
param (
$localPath = "U:\####\COKA_*", # Source, one or more files generated at on a request
$remotePath = "/from_/feast/Outbound/", #Destination file location
$backupPath = "U:\####\Archive", # archive file destination
)
try
{
# Load WinSCP .NET assembly
Add-Type -Path "E:\######\WinSCP\WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "123.456.78.901"
UserName = "iamencrypted"
SshHostKeyFingerprint = "ssh-rsa 2048 DYPA3BjRCbKLosI5W9iamdefinatlydencrypted"
SshPrivateKeyPath = "\\#####\###\###\##\FTP\######\#####\########.ppk"
}
$sessionOptions.AddRawSettings("AgentFwd", "1")
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files, collect results
$transferResult = $session.PutFiles($localPath, $remotePath)
# Iterate over every transfer
foreach ($transfer in $transferResult.Transfers)
{
# Success or error?
if ($transfer.Error -eq $Null)
{
Write-Host "Upload of $($transfer.FileName) succeeded, moving to Archive"
# Upload succeeded, move source file to Archive
Move-Item -force $transfer.FileName $backupPath
}
else
{
Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
那么你不能使用帶有文件掩碼的Session.PutFiles
。
您必須找到要自己上傳的文件,然后為每個文件分別調用Session.PutFiles
並在 ( Start-Sleep
) 之間暫停。
為此使用Get-ChildItem
。
$files = Get-ChildItem $localPath
foreach ($file in $files)
{
$localFilePath = $file.FullName
$transferResult = $session.PutFiles($localFilePath, $remotePath)
if ($transferResult.IsSuccess)
{
Write-Host "Upload of $localFilePath succeeded, moving to Archive"
# Upload succeeded, move source file to Archive
Move-Item -Force $localFilePath $backupPath
}
else
{
$err = $transferResult.Failures[0].Message
Write-Host "Upload of $localFilePath failed: $err"
}
Start-Sleep -Seconds 60
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.