Greetings,
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:
D:\TOUPLOAD\TESTDIR01
D:\TOUPLOAD\TESTDIR01\testfile01a.txt
D:\TOUPLOAD\TESTDIR01\testfile01b.txt
D:\TOUPLOAD\TESTDIR01\testfile01c.txt
D:\TOUPLOAD\TESTDIR02
D:\TOUPLOAD\TESTDIR02\testfile02a.txt
D:\TOUPLOAD\TESTDIR03\testfile02b.txt
D:\TOUPLOAD\TESTDIR03\testfile02c.txt
D:\TOUPLOAD\TESTDIR03
D:\TOUPLOAD\TESTDIR03\testfile03a.txt
D:\TOUPLOAD\TESTDIR03\testfile03b.txt
D:\TOUPLOAD\TESTDIR03\testfile03c.txt
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:
$transferResult.Transfers
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).