我有一个脚本只需从给定的ftp服务器(备份工具)下载所有文件。
一切正常,但需要几个小时才能完成,所以我决定根据WinSCP documentation添加一些进度信息:
function FileTransferProgress {
param($e)
if(($script:lastFileName -ne $Null) -and ($script:lastFileName -ne $e.FileName)) {
Write-Host
}
Write-Host -NoNewline ("`r`t{0} ({1:P0})" -f $e.FileName, $e.FileProgress)
$script:lastFileName = $e.FileName
}
$script:lastFileName = $Null
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = $ftpHostName
UserName = $ftpUserName
Password = $ftpPassword
}
...
$session.add_FileTransferProgress( { FileTransferProgress($_) } )
...
//start FTP session, open connection etc.
...
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$session.GetFiles($hostFolderPath, ($targetFolderPath + "\*"), $False, $transferOptions).Check()
//message about finished trasnfer
...
如果没有我的$session.add_FileTransferProgress( { FileTransferProgress($_) } )
,一切正常,但需要很长时间才能看到完成消息。使用add_FileTransferProgress
我无法传输任何文件,而是在几分钟后我总是最终得到:
错误:超时等待WinSCP响应
我不更改了工作代码中的任何其他内容(仅添加了FileTransferProgress
和functin FileTransferProgress
)。我想这个程序会在WinSCP通常会显示"计算目录"时崩溃。 (在传输文件之前)。
当我评论$session.add_FileTransferProgress( { FileTransferProgress($_) } )
时,一切正常。此外,使用$session.PutFiles
上传少量文件同时使用注释和未注释的行(它会输出上传的%进度)。
请求Martin Prikryl
:
我从开始运行此脚本 - > Powershell(作为管理员),然后cd
到它的位置和./script.ps1
(两种情况完全相同)。
我已经放置了:
$session.SessionLogPath = "I:\Sites\Temp\TransferLog.xml"
$session.DebugLogPath = "I:\Sites\Temp\TransferDebugLog.xml"
之前$session.open(...)
但只出现了调试日志(TransferDebugLog.xml
)(我发布了部分内容,因为它的文件很大):
Executing Assembly: WinSCPnet, Version=1.3.5.6958, Culture=neutral, PublicKeyToken=...; Path: I:\Sites\WinSCP-5.9.2-Automation\WinSCPnet.dll; Location: I:\Sites\WinSCP-5.9.2-Automation\WinSCPnet.dll; Product: 5.9.2.0
Entry Assembly:
Operating system: Microsoft Windows NT 10.0.14393.0
User: PolGraphic@...; Interactive: True
Runtime: 4.0.30319.42000
Console encoding: Input: Central European (DOS) (852); Output: Central European (DOS) (852)
Working directory: C:\Users\PolGraphic
Session.Open entering
...
Command: [open "ftp://...:***@... -passive=1 -timeout=15]
ExeSessionProcess.ExecuteCommand entering
ExeSessionProcess.ExecuteCommand leaving
ExeSessionProcess.ProcessEvent entering
ExeSessionProcess.ProcessInitEvent entering
ExeSessionProcess.ProcessInitEvent leaving
ExeSessionProcess.ProcessEvent leaving
ExeSessionProcess.ProcessEvent entering
ExeSessionProcess.ProcessTitleEvent entering
Not-supported title event [WinSCP]
ExeSessionProcess.ProcessTitleEvent leaving
...
ExeSessionProcess.ProcessEvent entering
ExeSessionProcess.ProcessInputEvent entering
Scheduling output: [winscp> open "ftp://polgraphic...@..." -passive=1 -timeout=15]
ExeSessionProcess.ProcessInputEvent leaving
ExeSessionProcess.ProcessEvent leaving
...
SessionLogReader.Read entering
Opening log without write sharing
Opening log with write sharing
Log opened
Skipping 0 nodes
Read node 1: XmlDeclaration xml=version="1.0" encoding="UTF-8"
...
SessionLogReader.Read entering
Read node 21: Element group
Log contents has not changed
SessionLogReader.Read leaving
Session.GetFiles entering
Session.GetFiles leaving
SessionLogReader.Read entering
Waiting for log update and dispatching events for 50
Waiting for log update and dispatching events for 100
Waiting for log update and dispatching events for 200
Waiting for log update and dispatching events for 400
Waiting for log update and dispatching events for 800
... //lots of "waiting for log update"
Waiting for log update and dispatching events for 800
Waiting for log update and dispatching events for 800
SessionLogReader.Read leaving
ProgressHandler.Dispose entering
Session.DisableProgressHandling entering
Session.DisableProgressHandling leaving
ProgressHandler.Dispose leaving
ElementLogReader.Dispose entering
ElementLogReader.ReadToEnd entering
SessionLogReader.Read entering
Waiting for log update and dispatching events for 50
SessionLogReader.Read leaving
ElementLogReader.ReadToEnd leaving
Swallowing exception
ElementLogReader.Dispose leaving
Session.GetFiles leaving
Session.Dispose entering
Session.Cleanup entering
Terminating process
Command: [exit]