Output from session.Putfiles eating 1 line from every dir!


Brussels, Belgium

Output from session.Putfiles eating 1 line from every dir!


First post here, I have searched the forums for this error but didn't find it. It concerns WinSCP with PowerShell so maybe it should be moved to the Scripting / Automation forum, but as I consider it a bug, I leave it here for the moment.

On a Win2008R2 SP1 server, I am using the WinSCP GUI to upload files to an sFTP server with a private key (PPK file) and it works perfectly.

Now I would like to script these uploads with PowerShell, and I am using the PowerShell Wrapper
On this page : https://winscp.net/eng/docs/library_powershell#module, I follow the link to download the wrapper. I include it successfully in PowerShell, and manage to use the code given in the "Example" chapter on that same page. It uploads the files just fine (I can check separately with the GUI) but the output (that I need to use for logging purposes) is bugging in this way:

The directory D:\TOUPLOAD contains the following folders & files:
I use the "D:\TOUPLOAD\*" parameter for the PutFiles method, just like in the Example.
All files are uploaded to the server, where the until-then-non-existing directories are created, with all files put accordingly, everything is actually working fine, I can confirm with the GUI!

BUT the output from the script is the following:
Upload of D:\TOUPLOAD\TESTDIR01\testfile01a.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR01\testfile01b.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR02\testfile02a.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR02\testfile02b.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR03\testfile03a.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR03\testfile03b.txt succeeded
Upload of D:\TOUPLOAD\TESTDIR03\testfile03c.txt succeeded
Look, the last file of all sub-directories but the last one is missing from the log.

I first thought it might be some PowerShell variable management issue but then I checked the content of the variable:
And the same files are missing.

Good question is: what happens if there is only one file in the directories? Well it gets eaten too ! Only the file(s) from the last directory show up in the output.

I can reproduce the issue at will, with any number of directories or files. Always the last file from all directories but the last directory, is missing.

This is really embarrassing when I need to use that output for other reasons afterwards (like moving files) so can you indicate me how to correct this or at least acknowledge it's a bug, 'cause I'm becoming crazy here :)

Summary :
  • Open-WinSCPSession outputs a Session object that we'll call $session.
  • The method $session.Putfiles (with correct parameters) uploads files to an sFTP server, and outputs a TransferOperationResult object (BaseType WinSCP.OperationResultBase). Let's call it $transfertResult
  • The property $transferResult.Transfers is a TransferEventArgsCollection , which has a FileName property.
  • I can find all uploaded files in there, except the last one of every directory, but the last directory (which has all its files).

Reply with quote

Brussels, Belgium

OK I managed to build a workaround.

Knowing that my directory structure is fixed (One root dir, a big amount of sub-dirs containing files, but no level deeper), I added the following:

 # $uploadDir_target & $transferOptions are defined previously in the code
$rootCI = gci "d:\TOUPLOAD"   # will contain all subdirs

        foreach ($ci in $rootCI)
         $transferResult = $session.PutFiles($ci.FullName, $uploadDir_target, $False, $transferOptions)

         foreach ($transfer in $transferResult.Transfers)
            Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)

Which is basically the same as the original script except I browse directory per directory, and as there is no sub-dir in those but only files, it works.

Still, this is very unstable, not portable, and if our directory structure ever goes deeper, I'm still in a situation (or I can do more get-childitem but this it not clean).

So even if for now I get what I want, the problem persists! :mrgreen:

Reply with quote


You can post new topics in this forum