WinSCP.exe and exit codes

Advertisement

tomeggers
Guest

WinSCP.exe and exit codes

WinSCP.exe, Build 9730

I'm using powershell to invoke WinSCP:
winscp.exe /script=.\winSCPscript.txt /log=".\logs\WinScpLog.txt"
The script has an open command:
open ftp://cowg_lists:"password"@ftp.abc.com:12345/
This works beautifully when the credentials are correct, and an exit code = 0 is returned to powershell. However,if the password is INCORRECT, an exit code = 0 is still returned to powershell instead of an exit code = 1.

If I use the powershell line (with the COM version):
winscp.com /script=.\winSCPscript.txt /log=".\logs\WinScpLog.txt"
It works correctly with the same script: exit code = 0 on success; and exit code = 1 on failure due to bad password.

My workaround is to use winscp.com, but that outputs a lot of extra stuff to the console that I can't find any way to suppress. So I would like winscp.exe to return an exit code = 1 on failures, or winscp.com to have a method to suppress console output.

Reply with quote

Advertisement

tomeggers
Joined:
Posts:
6
Location:
COS

more information on exit code post

Here is the complete script for both winscp.exe and winscp.com

option batch abort
open ftp://cowg_lists:"password"@ftp.abc.com:12345/
lcd tabfiles
put *.TAB
exit

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: WinSCP.exe and exit codes

This is PowerShell question, so it all depends on how you run WinSCP in PowerShell, what you didn't show us.

But this should work to suppress winscp.com output in PowerShell:

& winscp.com /script=.\winSCPscript.txt /log=".\logs\WinScpLog.txt" | Out-Null

Reply with quote

tomeggers

Indeed, piping to null solved my problem. Thank you.

But I still think WinSCP.exe should return an exit code = 1 if there is a failure. I described that in my first post (as a guest) in this topic.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

tomeggers wrote:

But I still think WinSCP.exe should return an exit code = 1 if there is a failure. I described that in my first post (as a guest) in this topic.
WinSCP does return exit code 1, when there is a failure.
If you have a problem with testing the exit code, show us your code.

Reply with quote

Advertisement

tomeggers
Joined:
Posts:
6
Location:
COS

>> WinSCP does return exit code 1, when there is a failure.
>> If you have a problem with testing the exit code, show us your code.
Fair enough.

Here is the powershell code:
winscp.exe /script=.\winTestScript.txt
Write-Output "WinSCP.exe ExitCode = $LastExitCode"

And here is the winscp.exe script (exactly one line):
open ftp://cowg_lists:"bad"@ftp.url.com:12345/

When this executes, the powershell output line is:
WinSCP.exe ExitCode =

Showing NOTHING after the equal sign, which shows that there is no exit code, not 0, not 1, nor anything else.

If I change the powershell code to:
winscp.com /script=.\winTestScript.txt
Write-Output "WinSCP.com ExitCode = $LastExitCode"
and use the exact same winscp script, the result is:
Connecting to ftp.url.com:12345 ...
Connection failed.
Timeout detected. (control connection)
Connection failed.
WinSCP.com ExitCode = 1

[Edited to remove a possible ambiguity:]
My conclusion is that WinSCP.com works correctly by returning an exit code = 1 for this error. WinSCP.exe does not return any exit code at all for the one-line script shown above. If the one-line script is modified so it works correctly, WinSCP.exe still does not return an exit code. WinSCP.exe should return an exit code in all cases.

Reply with quote

tomeggers
Joined:
Posts:
6
Location:
COS

To quote one of the stackoverflow postings, "brilliant".
I would never have figured that out on my own, it's counter intuitive.
And, in earlier post, you gave me a work-a-round.
Thank you.

Reply with quote

tomeggers
Joined:
Posts:
6
Location:
COS

Well, maybe not so brilliant.
I've read the stackoverflow topic several times, and I've read all of the replies. The one I like best suggests using this powershell cmdlet (adjusted for use here):

Start-Process -Wait winscp.exe /script=.\winTestScript.txt
Write-Output "WinSCP.exe ExitCode = $LastExitCode"

This also results in winscp.exe NOT returning any exit code, even when powershell is forced to wait by using the "start-process -wait" cmdlet.

I'm successfully using the work-a-round you gave me a few posts ago, so this is not a problem for me any longer. But it still appears that winscp.exe does not return an exit code. Do you think that winscp.exe should return an exit code? If so, I think you still have a bug to fix.

Reply with quote

Advertisement

tomeggers
Joined:
Posts:
6
Location:
COS

Yes, your last reference to stackoverflow supplies a good answer. Here is working and tested powershell code:

$P = Start-Process -Wait -PassThru winscp.exe /script=.\winTestScript.txt
$P.ExitCode

The problem (and fix) is entirely with powershell, not with WinSCP.exe.
Thank you for the reference.

Reply with quote

Advertisement

You can post new topics in this forum