I have an script that simply download all the files from given ftp server (backup tool).
Everything works fine but it takes hours before complete so I've decided to add some progress info based on 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
...
Without my $session.add_FileTransferProgress( { FileTransferProgress($_) } )
everything works fine but it takes a long time to see the finish message. With add_FileTransferProgress
I don't get any files transfered and instead, after few minutes I always end up with:
Error: Timeout waiting for WinSCP to respond
I've not changed anything else in the working code (only added FileTransferProgress
and functin FileTransferProgress
). I guess that program crashes when normally WinSCP would show "calculating directories" (before transfering files).
When I comment out $session.add_FileTransferProgress( { FileTransferProgress($_) } )
everything works fine. Also, uploading of few files with $session.PutFiles
works both with commented and un-commented line (it outputs the % progress for upload).
As Martin Prikryl
requested:
I run this script from Start -> Powershell (as administrator) and then cd
to where it is placed and ./script.ps1
(exactly the same way for both cases).
I've placed:
$session.SessionLogPath = "I:\Sites\Temp\TransferLog.xml"
$session.DebugLogPath = "I:\Sites\Temp\TransferDebugLog.xml"
before $session.open(...)
but only the debug log (TransferDebugLog.xml
) appeared (I post parts of it, because it's huge file):
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]