exit code 1 on script

Advertisement

habsgoalie
Joined:
Posts:
4

exit code 1 on script

I am trying to execute a script which results in a successful connection and transfer, but returns an error code of 1.

Here is the script-

open ftp://ftpuser:password@ftp.domain.net
option batch abort
option confirm off
put \\domain.net\common\share\IT_Operations\ftptest\*.txt
exit

And here is the console output-

Connecting to ftp.domain.net ...
Connected with ftp.domain.net. Waiting for welcome message...
Connected
Starting the session...
Reading remote directory...
Session started.
Active session: [1] ftpuser@ftp.domain.net
batch abort
confirm off
\\domain.net\common\share\IT_Operations\ftptest\foo.txt | 0 KiB | 0.0 KiB/s | ascii | 0%


And here is the log-


. 2012-05-18 16:57:26.756 --------------------------------------------------------------------------
. 2012-05-18 16:57:26.756 WinSCP Version 5.0.7 (Build 2268) (OS 6.1.7601 Service Pack 1)
. 2012-05-18 16:57:26.756 Configuration: C:\Program Files (x86)\WinSCP\WinSCP.ini
. 2012-05-18 16:57:26.757 Local account: xxxxx
. 2012-05-18 16:57:26.757 Login time: Friday, May 18, 2012 4:57:26 PM
. 2012-05-18 16:57:26.757 --------------------------------------------------------------------------
. 2012-05-18 16:57:26.757 Session name: ftpuser@ftp.domain.net (Ad-Hoc session)
. 2012-05-18 16:57:26.757 Host name: ftp.domain.net (Port: 21)
. 2012-05-18 16:57:26.757 User name: ftpuser (Password: Yes, Key file: No)
. 2012-05-18 16:57:26.757 Tunnel: No
. 2012-05-18 16:57:26.757 Transfer Protocol: FTP
. 2012-05-18 16:57:26.757 Ping type: C, Ping interval: 30 sec; Timeout: 15 sec
. 2012-05-18 16:57:26.757 Proxy: none
. 2012-05-18 16:57:26.757 FTP: FTPS: None; Passive: Yes [Force IP: No]
. 2012-05-18 16:57:26.757 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2012-05-18 16:57:26.757 Cache directory changes: Yes, Permanent: Yes
. 2012-05-18 16:57:26.757 DST mode: 1
. 2012-05-18 16:57:26.757 --------------------------------------------------------------------------
. 2012-05-18 16:57:26.764 Connecting to ftp.domain.net ...
. 2012-05-18 16:57:26.767 Connected with ftp.domain.net. Waiting for welcome message...
< 2012-05-18 16:57:26.767 220 Microsoft FTP Service
> 2012-05-18 16:57:26.767 USER ftpuser
< 2012-05-18 16:57:26.768 331 Password required for ftpuser.
> 2012-05-18 16:57:26.768 PASS *********
< 2012-05-18 16:57:26.769 230 User ftpuser logged in.
> 2012-05-18 16:57:26.769 SYST
< 2012-05-18 16:57:26.769 215 Windows_NT
> 2012-05-18 16:57:26.769 FEAT
< 2012-05-18 16:57:26.771 211-FEAT
< 2012-05-18 16:57:26.968 SIZE
< 2012-05-18 16:57:26.968 MDTM
< 2012-05-18 16:57:26.968 211 END
. 2012-05-18 16:57:26.971 Connected
. 2012-05-18 16:57:26.971 --------------------------------------------------------------------------
. 2012-05-18 16:57:26.971 Using FTP protocol.
. 2012-05-18 16:57:26.972 Doing startup conversation with host.
> 2012-05-18 16:57:26.972 PWD
< 2012-05-18 16:57:26.973 257 "/ftpuser" is current directory.
. 2012-05-18 16:57:26.974 Getting current directory name.
. 2012-05-18 16:57:26.974 Startup conversation with host finished.
< 2012-05-18 16:57:26.975 Script: Active session: [1] ftpuser@ftp.domain.net
> 2012-05-18 16:57:26.975 Script: option batch abort
< 2012-05-18 16:57:26.976 Script: batch abort
> 2012-05-18 16:57:26.976 Script: option confirm off
< 2012-05-18 16:57:26.976 Script: confirm off
> 2012-05-18 16:57:26.977 Script: put \\domain.net\common\share\IT_Operations\ftptest\*.txt
. 2012-05-18 16:57:26.978 Copying 1 files/directories to remote directory "/ftpuser"
. 2012-05-18 16:57:26.978 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask:
. 2012-05-18 16:57:26.978 TM: M; ClAr: No; CPS: 0; InclM:
. 2012-05-18 16:57:26.978 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2012-05-18 16:57:26.978 File: "\\domain.net\common\share\IT_Operations\ftptest\foo.txt"
. 2012-05-18 16:57:26.980 Copying "\\domain.net\common\share\IT_Operations\ftptest\foo.txt" to remote directory started.
. 2012-05-18 16:57:26.981 Ascii transfer mode selected.
. 2012-05-18 16:57:26.981 Starting upload of \\domain.net\common\share\IT_Operations\ftptest\foo.txt
> 2012-05-18 16:57:26.981 TYPE A
< 2012-05-18 16:57:26.981 200 Type set to A.
> 2012-05-18 16:57:26.981 PASV
< 2012-05-18 16:57:26.982 227 Entering Passive Mode (10,95,201,21,19,75).
> 2012-05-18 16:57:26.983 LIST
< 2012-05-18 16:57:26.984 125 Data connection already open; Transfer starting.
< 2012-05-18 16:57:27.188 226 Transfer complete.
> 2012-05-18 16:57:27.195 TYPE A
< 2012-05-18 16:57:27.196 200 Type set to A.
> 2012-05-18 16:57:27.196 PASV
< 2012-05-18 16:57:27.197 227 Entering Passive Mode (10,95,201,21,19,76).
> 2012-05-18 16:57:27.199 STOR foo.txt
< 2012-05-18 16:57:27.200 125 Data connection already open; Transfer starting.
< 2012-05-18 16:57:27.398 226 Transfer complete.
. 2012-05-18 16:57:27.398 Upload successful
> 2012-05-18 16:57:27.398 Script: exit
. 2012-05-18 16:57:27.401 Disconnected from server

Any idea why I am getting exit code 1 as opposed to exit code 0? This batch script is part of a larger powershell script with conditional actions firing on non-0 exit code, so I am eager to resolve this. Please let me know if there are any suggestions. In addition to trying this with version 5.0.7, I have also tried 4.3.7.

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: exit code 1 on script

It does not look like The script fails. It would say so in the log. Can you post piece of code you are running WinSCP and checking the exit code with?

Reply with quote

habsgoalie
Joined:
Posts:
4

#*Delcare File path variables
$filepath = "\\domain.net\common\share\IT_Operations\ftptest"
$archivepath = "\\domain.net\common\share\ftptest\archive"

#*Declare paramaters for WinSCP command
$parmfile = "C:\Users\user\Documents\Scripts\ftpparms.txt"
$parm = "/script=$parmfile /log=c:\winscp.log"
$winscp = "C:\Program Files (x86)\WinSCP\WinSCP.com"

#*Declare email variables
$recipient = "user@domain.net"
$sender = "admin@domain.com"
$mailserver = "smtp.domain.net"

#*Variables for error handling
$resultfile = "C:\Users\user\Documents\Scripts\ftptest.txt"
$filecount = @(Get-ChildItem $filepath *.txt).Count

#* check for existence of more than one file, generate alert if more than one file is in the directory
if ($filecount -gt 1)
{
Send-Mailmessage -to $recipient -Subject "ftptest error" -from $sender -Body "Multiple Payment files in the folder" -SmtpServer $mailserver
}
else
{
#*If only one file is found, start SFTP transfer
Start-process $winscp "$($parm)" -RedirectStandardOutput $resultfile -wait

#*error handling for the SFTP transfer
if ($lastexicode -ne 0) 
{
$body = (Get-Content $resultfile | Out-String)
Send-Mailmessage -to $recipient -Subject "ftptest error" -from $sender -Body "$body" -SmtpServer $mailserver
}
elseif
{

#*Move the file to archive once it is successfully transferred
set-location $filepath
Move-Item *.txt $archivepath
if ($errorcount -ne 0)
    {
    #*generate an alert if move to archive fails
    Send-Mailmessage -to $recipient -Subject "ftptest error" -from $sender -Body "Error moving Payment Recon file to archive folder" -SmtpServer $mailserver
    }
}
}
[/code]

Reply with quote

martin
Site Admin
martin avatar

I have sent you an email with a development version of WinSCP to address you have used to register on this forum.

Reply with quote

habsgoalie
Joined:
Posts:
4

martin wrote:

I have sent you an email with a development version of WinSCP to address you have used to register on this forum.

Hello,

I tried this again with the version of WinSCP you sent me. It did not return any value for an exit code this time, though perhaps that was intentional on your part, since the exit code now appears in the log file.

No exit code:

PS C:\Scripts> $lastexitcode -eq $null
True

Log File:


. 2012-05-22 14:32:45.449 --------------------------------------------------------------------------
. 2012-05-22 14:32:45.449 WinSCP Version 5.0.7 (Build 2304) (OS 6.1.7601 Service Pack 1)
. 2012-05-22 14:32:45.450 Configuration: C:\Program Files (x86)\WinSCP\WinSCP.ini
. 2012-05-22 14:32:45.450 Local account: domainUS\user
. 2012-05-22 14:32:45.450 Login time: Tuesday, May 22, 2012 2:32:45 PM
. 2012-05-22 14:32:45.450 --------------------------------------------------------------------------
. 2012-05-22 14:32:45.450 Session name: ftpuser@ftp.domain.net (Ad-Hoc session)
. 2012-05-22 14:32:45.450 Host name: ftp.domain.net (Port: 21)
. 2012-05-22 14:32:45.450 User name: ftpuser (Password: Yes, Key file: No)
. 2012-05-22 14:32:45.450 Tunnel: No
. 2012-05-22 14:32:45.450 Transfer Protocol: FTP
. 2012-05-22 14:32:45.450 Ping type: C, Ping interval: 30 sec; Timeout: 15 sec
. 2012-05-22 14:32:45.450 Proxy: none
. 2012-05-22 14:32:45.450 FTP: FTPS: None; Passive: Yes [Force IP: No]
. 2012-05-22 14:32:45.450 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2012-05-22 14:32:45.450 Cache directory changes: Yes, Permanent: Yes
. 2012-05-22 14:32:45.450 DST mode: 1
. 2012-05-22 14:32:45.450 --------------------------------------------------------------------------
. 2012-05-22 14:32:45.457 Connecting to ftp.domain.net ...
. 2012-05-22 14:32:45.460 Connected with ftp.domain.net. Waiting for welcome message...
< 2012-05-22 14:32:45.460 220 Microsoft FTP Service
> 2012-05-22 14:32:45.460 USER ftpuser
< 2012-05-22 14:32:45.461 331 Password required for ftpuser.
> 2012-05-22 14:32:45.461 PASS *********
< 2012-05-22 14:32:45.462 230 User ftpuser logged in.
> 2012-05-22 14:32:45.462 SYST
< 2012-05-22 14:32:45.462 215 Windows_NT
> 2012-05-22 14:32:45.462 FEAT
< 2012-05-22 14:32:45.463 211-FEAT
< 2012-05-22 14:32:45.660 SIZE
< 2012-05-22 14:32:45.660 MDTM
< 2012-05-22 14:32:45.660 211 END
. 2012-05-22 14:32:45.661 Connected
. 2012-05-22 14:32:45.661 --------------------------------------------------------------------------
. 2012-05-22 14:32:45.661 Using FTP protocol.
. 2012-05-22 14:32:45.662 Doing startup conversation with host.
> 2012-05-22 14:32:45.663 PWD
< 2012-05-22 14:32:45.664 257 "/ftpuser" is current directory.
. 2012-05-22 14:32:45.664 Getting current directory name.
. 2012-05-22 14:32:45.665 Startup conversation with host finished.
< 2012-05-22 14:32:45.665 Script: Active session: [1] ftpuser@ftp.domain.net
> 2012-05-22 14:32:45.666 Script: option batch abort
< 2012-05-22 14:32:45.666 Script: batch abort
> 2012-05-22 14:32:45.667 Script: option confirm off
< 2012-05-22 14:32:45.667 Script: confirm off
> 2012-05-22 14:32:45.667 Script: put \\domain.net\common\share\IT_Operations\ftptest\*.txt
. 2012-05-22 14:32:45.720 Copying 1 files/directories to remote directory "/ftpuser"
. 2012-05-22 14:32:45.720 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask:
. 2012-05-22 14:32:45.720 TM: M; ClAr: No; CPS: 0; InclM:
. 2012-05-22 14:32:45.720 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2012-05-22 14:32:45.758 File: "\\domain.net\common\share\IT_Operations\ftptest\foo.txt"
. 2012-05-22 14:32:45.760 Copying "\\domain.net\common\share\IT_Operations\ftptest\foo.txt" to remote directory started.
. 2012-05-22 14:32:45.761 Ascii transfer mode selected.
. 2012-05-22 14:32:45.761 Starting upload of \\domain.net\common\share\IT_Operations\ftptest\foo.txt
> 2012-05-22 14:32:45.761 TYPE A
< 2012-05-22 14:32:45.761 200 Type set to A.
> 2012-05-22 14:32:45.761 PASV
< 2012-05-22 14:32:45.762 227 Entering Passive Mode (10,95,201,21,18,219).
> 2012-05-22 14:32:45.763 LIST
< 2012-05-22 14:32:45.764 125 Data connection already open; Transfer starting.
< 2012-05-22 14:32:45.963 226 Transfer complete.
> 2012-05-22 14:32:45.971 TYPE A
< 2012-05-22 14:32:45.972 200 Type set to A.
> 2012-05-22 14:32:45.972 PASV
< 2012-05-22 14:32:45.972 227 Entering Passive Mode (10,95,201,21,18,220).
> 2012-05-22 14:32:45.975 STOR foo.txt
< 2012-05-22 14:32:45.976 125 Data connection already open; Transfer starting.
< 2012-05-22 14:32:46.174 226 Transfer complete.
. 2012-05-22 14:32:46.174 Upload successful
> 2012-05-22 14:32:46.174 Script: exit
. 2012-05-22 14:32:46.174 Script: Exit code: 0
. 2012-05-22 14:32:46.177 Disconnected from server
[/code]

Reply with quote

Advertisement

habsgoalie
Joined:
Posts:
4

Found the issue

Hello,

Thanks for your help. I found the problem, and it turned out to be a problem with the Powershell code I am using to execute winscp.com.

For the benefit of anyone else that may run into this- If you launch winscp.com within powershell, or any external executable for that matter, using Start-Process- The exit code is not stored in the environment variable $lastexitcode. THis is why my script was failing. I believe when I first reported the trouble I was observing a $lastexitcode value of 1 based on another command I had launched earlier in the same session, and the exit code was still stored.

The work around was to change this-

#*If only one file is found, start SFTP transfer 
Start-process $winscp "$($parm)" -RedirectStandardOutput $resultfile -wait 

#*error handling for the SFTP transfer 
if ($lastexicode -ne 0)

to this:

#*If only one file is found, start SFTP transfer
$proc = Start-process $winscp "$($parm)" -RedirectStandardOutput $resultfile -wait -Passthru
$ExitCode = $proc.ExitCode 
$proc

#*error handling for the SFTP transfer
if ($ExitCode -ne 0)

Once I did that, everything worked fine!

Thanks again for your help!

Reply with quote

Advertisement

You can post new topics in this forum