Batch file Email Alerts on failures or errors?

Advertisement

1DA
Joined:
Posts:
6

Batch file Email Alerts on failures or errors?

Is it possible to have a WinSCP batch file script send an email? If for instance a file fails to upload? I currently upload multiple files, but I want an alert if even 1 fails or an error occurs. Is this possible? Thanks in advance.

Reply with quote

Advertisement

1DA
Joined:
Posts:
6

Can't figure out what I'm doing wrong.

Using example script:

\\Generic\Generic\winscp.com /ini=nul /script=F:\Generic\Generic\Generic\WinSCP.log
if %ERRORLEVEL% neq 0 goto error

echo Success
\\Generic\Generic\sendmail.exe -t < \\Generic\Generic\\Success.txt
:exit /b 0

:error
echo Error!
\\Generic\Generic\sendmail.exe -t < \\Generic\Generic\\Fail.txt
:exit /b 1

pause



I get the error "." is not a command line. I have WinSCP and sendmail.exe in the same folder.
If I remove the winscp.com path and just leave it as is, I get a "winscp.com" is not a recognized command windows error. What is this code supposed to look like exactly, the website does not explain this. I have the : in place on the exits so I can see what is causing an error for testing.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
31,243
Location:
Prague, Czechia

Can you post a screenshot of your "." is not a command line error?

Reply with quote

1DA
Joined:
Posts:
6

Thanks for the reply, I was able to get this going by just plugging in

\\Generic\Generic\sendmail.exe -t < \\Generic\Generic\\Fail.txt

into the Generated code of a file transfer in winscp after the error script. This worked a lot better and was easier to understand than what the tutorial was trying to explain. Now that it works and sends an email on a fail. Is there a way to make explicit error emails?
Example right now, if 1 file upload fails out of 20, it still considers it a successful upload. I would like even 1 failure of an uploaded file to send an email alert.

Right now I'm using.

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
\\Generic\Generic\WinSCP\sendmail.exe -t < \\Generic\Generic\WinSCP\Fail.txt
)

exit /b %WINSCP_RESULT%

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
31,243
Location:
Prague, Czechia

1DA wrote:

Example right now, if 1 file upload fails out of 20, it still considers it a successful upload. I would like even 1 failure of an uploaded file to send an email alert.
It should not behave this way.
Please post your full script as well as WinSCP session log file.

Reply with quote

Advertisement

1DA
Joined:
Posts:
6

I've had to exclude some links but this is the base of the Winscp log that shows an error but did not send out an error email. I've turned off partial file transfers for now to help with this.

winscp Log
. 2019-02-20 00:07:20.859 File: 'Generic.txt' [2019-02-20T08:07:16.087Z] [7148863]
. 2019-02-20 00:07:20.859 Copying "Generic.txt" to remote directory started.
. 2019-02-20 00:07:20.859 Binary transfer mode selected.
. 2019-02-20 00:07:20.859 Checking existence of file.
> 2019-02-20 00:07:20.859 Type: SSH_FXP_LSTAT, Size: 63, Number: 61703
< 2019-02-20 00:07:20.906 Type: SSH_FXP_STATUS, Size: 24, Number: 22790
< 2019-02-20 00:07:20.906 Status code: 0
< 2019-02-20 00:07:20.906 Type: SSH_FXP_STATUS, Size: 24, Number: 23046
< 2019-02-20 00:07:20.906 Status code: 0
< 2019-02-20 00:07:20.906 Type: SSH_FXP_STATUS, Size: 24, Number: 23302
< 2019-02-20 00:07:20.906 Status code: 0
< 2019-02-20 00:07:20.906 Type: SSH_FXP_STATUS, Size: 24, Number: 23556
< 2019-02-20 00:07:20.906 Status code: 0
> 2019-02-20 00:07:20.906 Type: SSH_FXP_REMOVE, Size: 63, Number: 23821
< 2019-02-20 00:07:20.922 Type: SSH_FXP_ATTRS, Size: 37, Number: 61703
. 2019-02-20 00:07:20.937 File exists: Generic.txt;-;7122363;2019-02-20T08:07:20.000Z;3;"" [1001];"" [1013];rw-r--r--;0
. 2019-02-20 00:07:20.937 Checking existence of partially transferred file.
> 2019-02-20 00:07:20.937 Type: SSH_FXP_LSTAT, Size: 72, Number: 61959
< 2019-02-20 00:07:20.984 Type: SSH_FXP_STATUS, Size: 24, Number: 23821
< 2019-02-20 00:07:20.984 Status code: 0
> 2019-02-20 00:07:20.984 Type: SSH_FXP_RENAME, Size: 130, Number: 24082
< 2019-02-20 00:07:21.000 Type: SSH_FXP_STATUS, Size: 29, Number: 61959
< 2019-02-20 00:07:21.000 Status code: 2
. 2019-02-20 00:07:21.000 Opening remote file.
> 2019-02-20 00:07:21.000 Type: SSH_FXP_OPEN, Size: 88, Number: 62211
< 2019-02-20 00:07:21.047 Type: SSH_FXP_STATUS, Size: 29, Number: 24082
< 2019-02-20 00:07:21.047 Status code: 2, Message: 24082, Server: No such file, Language:
* 2019-02-20 00:07:21.062 (ETerminal) No such file or directory.
* 2019-02-20 00:07:21.062 Error code: 2
* 2019-02-20 00:07:21.062 Error message from server: No such file
. 2019-02-20 00:07:21.062 Asking user:
. 2019-02-20 00:07:21.062 Transfer was successfully finished, but temporary transfer file 'Generic.txt.filepart' could not be renamed to target file name 'Generic.txt'. If the problem persists, you may try to turn off transfer resume support. ("No such file or directory.
. 2019-02-20 00:07:21.062 Error code: 2
. 2019-02-20 00:07:21.062 Error message from server: No such file")
< 2019-02-20 00:07:21.062 Script: Transfer was successfully finished, but temporary transfer file 'Generic.txt.filepart' could not be renamed to target file name 'Generic.txt'. If the problem persists, you may try to turn off transfer resume support.
< 2019-02-20 00:07:21.062 Script: No such file or directory.
< 2019-02-20 00:07:21.062 Error code: 2
< 2019-02-20 00:07:21.062 Error message from server: No such file
* 2019-02-20 00:07:21.062 (ESkipFile) Transfer was successfully finished, but temporary transfer file 'Generic.txt.filepart' could not be renamed to target file name 'Generic.txt'. If the problem persists, you may try to turn off transfer resume support.
* 2019-02-20 00:07:21.062 No such file or directory.
* 2019-02-20 00:07:21.062 Error code: 2
* 2019-02-20 00:07:21.062 Error message from server: No such file
< 2019-02-20 00:07:21.078 Type: SSH_FXP_HANDLE, Size: 13, Number: 62211
> 2019-02-20 00:07:21.078 Type: SSH_FXP_WRITE, Size: 32764, Number: 62726
. 2019-02-20 00:07:21.078 Copying finished: Transferred: 2,609,689, Elapsed: 0:00:01, CPS: 1,874,145/s
. 2019-02-20 00:07:21.078 Script: Failed
. 2019-02-20 00:07:21.078 Script: Exit code: 1
. 2019-02-20 00:07:21.078 Closing connection.
. 2019-02-20 00:07:21.078 Sending special code: 12
. 2019-02-20 00:07:21.078 Sent EOF message
. 2019-02-20 00:07:21.531 403 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2019-02-20 00:07:21.531 Type: SSH_FXP_CLOSE, Size: 13, Number: 118788
< 2019-02-20 00:07:21.531 Type: SSH_FXP_STATUS, Size: 24, Number: 110086
< 2019-02-20 00:07:21.531 Status code: 0
< 2019-02-20 00:07:21.531 Type: SSH_FXP_STATUS, Size: 24, Number: 110342
< 2019-02-20 00:07:21.531 Status code: 0
< 2019-02-20 00:07:21.531 Type: SSH_FXP_STATUS, Size: 24, Number: 110598
< 2019-02-20 00:07:21.531 Status code: 0
< 2019-02-20 00:07:21.531 Type: SSH_FXP_STATUS, Size: 24, Number: 110854
< 2019-02-20 00:07:21.531 Status code: 0
< 2019-02-20 00:07:21.531 Type: SSH_FXP_STATUS, Size: 24, Number: 111110
< 2019-02-20 00:07:21.531 Status code: 0
< 2019-02-20 00:07:21.531 Type: SSH_FXP_STATUS, Size: 24, Number: 111366
< 2019-02-20 00:07:21.531 Status code: 0
< 2019-02-20 00:07:21.531 Type: SSH_FXP_STATUS, Size: 24, Number: 111622
< 2019-02-20 00:07:21.531 Status code: 0
< 2019-02-20 00:07:21.547 Type: SSH_FXP_STATUS, Size: 24, Number: 111878
< 2019-02-20 00:07:21.547 Status code: 0
< 2019-02-20 00:07:21.562 Type: SSH_FXP_STATUS, Size: 24, Number: 112134
< 2019-02-20 00:07:21.562 Status code: 0
< 2019-02-20 00:07:21.562 Type: SSH_FXP_STATUS, Size: 24, Number: 112390
< 2019-02-20 00:07:21.562 Status code: 0
< 2019-02-20 00:07:21.562 Type: SSH_FXP_STATUS, Size: 24, Number: 112646
< 2019-02-20 00:07:21.562 Status code: 0
< 2019-02-20 00:07:21.562 Type: SSH_FXP_STATUS, Size: 24, Number: 112902
< 2019-02-20 00:07:21.562 Status code: 0
< 2019-02-20 00:07:21.562 Type: SSH_FXP_STATUS, Size: 24, Number: 113158
< 2019-02-20 00:07:21.562 Status code: 0
< 2019-02-20 00:07:21.562 Type: SSH_FXP_STATUS, Size: 24, Number: 113414
< 2019-02-20 00:07:21.562 Status code: 0
< 2019-02-20 00:07:21.562 Type: SSH_FXP_STATUS, Size: 24, Number: 113670
< 2019-02-20 00:07:21.562 Status code: 0
< 2019-02-20 00:07:21.578 Type: SSH_FXP_STATUS, Size: 24, Number: 113926
< 2019-02-20 00:07:21.578 Status code: 0
< 2019-02-20 00:07:21.578 Type: SSH_FXP_STATUS, Size: 24, Number: 114182
< 2019-02-20 00:07:21.578 Status code: 0
< 2019-02-20 00:07:21.578 Type: SSH_FXP_STATUS, Size: 24, Number: 114438
< 2019-02-20 00:07:21.578 Status code: 0
< 2019-02-20 00:07:21.578 Type: SSH_FXP_STATUS, Size: 24, Number: 114694
< 2019-02-20 00:07:21.578 Status code: 0
< 2019-02-20 00:07:21.578 Type: SSH_FXP_STATUS, Size: 24, Number: 114950
< 2019-02-20 00:07:21.578 Status code: 0
< 2019-02-20 00:07:21.578 Type: SSH_FXP_STATUS, Size: 24, Number: 115206
< 2019-02-20 00:07:21.578 Status code: 0
< 2019-02-20 00:07:21.578 Type: SSH_FXP_STATUS, Size: 24, Number: 115462
< 2019-02-20 00:07:21.578 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 115718
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 115974
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 116230
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 116486
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 116742
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 116998
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 117254
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 117510
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 117766
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 118022
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 118278
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 118534
< 2019-02-20 00:07:21.594 Status code: 0
< 2019-02-20 00:07:21.594 Type: SSH_FXP_STATUS, Size: 24, Number: 118788
< 2019-02-20 00:07:21.594 Status code: 0
> 2019-02-20 00:07:21.594 Type: SSH_FXP_REMOVE, Size: 63, Number: 119053
< 2019-02-20 00:07:21.672 Type: SSH_FXP_STATUS, Size: 29, Number: 119053
< 2019-02-20 00:07:21.672 Status code: 2, Message: 119053, Server: No such file, Language:
* 2019-02-20 00:07:21.672 (ETerminal) No such file or directory.
* 2019-02-20 00:07:21.672 Error code: 2
* 2019-02-20 00:07:21.672 Error message from server: No such file
. 2019-02-20 00:07:21.672 Asking user:
. 2019-02-20 00:07:21.672 Error deleting file 'Generic.txt'. After resumable file upload the existing destination file must be deleted. If you do not have permissions to delete file destination file, you need to disable resumable file transfers. ("No such file or directory.
. 2019-02-20 00:07:21.672 Error code: 2
. 2019-02-20 00:07:21.672 Error message from server: No such file")
< 2019-02-20 00:07:21.672 Script: Error deleting file 'Generic.txt'. After resumable file upload the existing destination file must be deleted. If you do not have permissions to delete file destination file, you need to disable resumable file transfers.
< 2019-02-20 00:07:21.672 Script: No such file or directory.
< 2019-02-20 00:07:21.672 Error code: 2
< 2019-02-20 00:07:21.672 Error message from server: No such file
* 2019-02-20 00:07:21.672 (ESkipFile) Error deleting file 'Generic.txt'. After resumable file upload the existing destination file must be deleted. If you do not have permissions to delete file destination file, you need to disable resumable file transfers.
* 2019-02-20 00:07:21.687 No such file or directory.
* 2019-02-20 00:07:21.687 Error code: 2
* 2019-02-20 00:07:21.687 Error message from server: No such file
. 2019-02-20 00:07:21.687 Copying finished: Transferred: 7,193,149, Elapsed: 0:00:01, CPS: 7,389,484/s
. 2019-02-20 00:07:21.687 Script: Failed
. 2019-02-20 00:07:21.687 Script: Exit code: 1
. 2019-02-20 00:07:21.687 Closing connection.
. 2019-02-20 00:07:21.687 Sending special code: 12
. 2019-02-20 00:07:21.687 Sent EOF message


This is the script we are using to upload, minus the server information.

" "lcd \\1.1.1.1\generic\generic\FILES" ^
"cd /home/generic/generic/generic/" ^
"put ""*.txt""" ^
"put -delete "*.txt"" ^
"exit"

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
\\generic\generic\FILES\WinSCP\sendmail.exe -t < \\generic\generic\FILES\WinSCP\Fail.txt
)

exit /b %WINSCP_RESULT%

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
31,243
Location:
Prague, Czechia

As you can see, WinSCP returned exit code 1:
. 2019-02-20 00:07:21.687 Script: Exit code: 1
So it behaves correctly.
It must be your batch file that incorrectly processes the exit code.
But you didn't post your complete batch file, so I cannot tell why.

Reply with quote

Advertisement

You can post new topics in this forum