[英]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.