【发布时间】:2021-02-23 20:46:42
【问题描述】:
所以我有一个简单的 FTP 上传脚本,它循环通过一组文件上传。循环选择一个文件(排序的 gci 数组)上传它,然后继续下一个。总共大约20个文件。当我从服务器收到错误或从服务器确认上传已完成时,单个文件的循环结束。
最近,该服务器也没有给我,允许上传通过,但没有确认文件已完成。从本质上讲,脚本只是在这个循环中停留了几个小时,直到我手动关闭脚本。
我希望在主上传命令上设置一个超时,即:
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
似乎写命令,虽然文件已完全写入目标 FTP 服务器,但从未从 FTP 服务器得到响应,这意味着它只会挂起挂起。
我希望做的是添加超时。这里的挑战是,我不知道如何在执行“写入”作业的过程中检查计时器。
我可以在这里选择吗?
这就是我要做的,但似乎(我的猜测)因为在写入作业的中间永远不会对计时器进行相互比较,循环不会退出,直到 re.write 作业完成(这可能永远不会)
这里有什么想法吗?
$timeout = New-TimeSpan -Seconds 8
$timer = [System.Diagnostics.Stopwatch]::StartNew()
do {
# create the FtpWebRequest and configure it
$ftp = [System.Net.FtpWebRequest]::Create($ftpdestination)
$ftp = [System.Net.FtpWebRequest]$ftp
# build authentication and connection
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential($username,$password)
$ftp.UseBinary = $true
$ftp.UsePassive = $true
$ftp.timeout = -1
# read in the file to upload as a byte array
$content = [System.IO.File]::ReadAllBytes($sourcefile)
$ftp.ContentLength = $content.Length
# get the request stream, and write the bytes into it
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
$rs.Close()
$rs.Dispose()
}
while ($timer.Elapsed -lt $timeout)
所以这就是我正在使用的,但是,我知道问题是在rs.write(我所有的时间都在)期间,它没有重新评估 ($timer.Elapsed -lt $timeout) 方程,所以它没有停止。这就是我卡住的地方。
【问题讨论】:
标签: powershell loops ftp ftpwebrequest