• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

WinSCP Scripting Problem

I am using WinSCP script to SFTP a file.  I have the code working to upload the file but I'm wanting to send a confirmation email upon success/failure of the transfer.    Here is what I've done so far:

Created a batch file (that is called weekly through Windows Scheduler).


@echo on
"C:\Program Files (x86)\WinSCP\WinSCP.exe" /script=c:\netlearning\nl_upload.txt

Calls nl_upload.txt script file (contents here)

option batch abort
# Disable overwrite confirmations that conflict with the previous
option confirm off
# Connect to SFTP server using a password
open sftp://username:password@sft.site.com/ -hostkey="ssh-rsa 2048 xx:xx:xx:xx.......
# Upload file
put c:\folder\somefile.csv
# Disconnect

This works correctly and the file is uploaded.   how do I then grab a 'successs/failure' parameter to send via email?  I was thinking about grabbing the parameter and then passing it to PowerShell to a script I wrote to send an email....  (or is there an easier way....

powershell script to send email:

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient("smtp_email_server")
$msg.From = "sender@somesite.org"
$msg.Subject = "Success/Failure"
$msg.Body = "Success/Failure"

Thank you!
  • 2
  • 2
2 Solutions
Gerwin Jansen, EE MVETopic Advisor Commented:
Redirect the output of your schedule script to a file, then use findstr to find your success string.  Based on what you find send the mail.
BCHCAdminAuthor Commented:
Ok - figured out how to write the results to a text file but having issues using the if statement in powershell to make a determination.  I'm writing a "0"on success to results.txt and "1" on failure to file with same name,.  Can you help with powershell commands?
Gerwin Jansen, EE MVETopic Advisor Commented:
>> using the if statement in powershell to make a determination
Do the if in your winscp_start.bat file instead:

findstr /i somestring yourtextfile >nul:
:: put your powershell mail command here
) else (
echo "Everything OK"
BCHCAdminAuthor Commented:
Ended up using the PowerShell Script to open and read the file but thank you for getting me in the right direction!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now