Solved

WinSCP Scripting Problem

Posted on 2014-12-04
4
283 Views
Last Modified: 2014-12-11
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).

WinSCP_Start.bat

@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
#close

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.To.Add("receiver@somesite.org")
$msg.Subject = "Success/Failure"
$msg.Body = "Success/Failure"
$smtp.Send($msg)


Thank you!
0
Comment
Question by:BCHCAdmin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 40481505
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.
0
 

Author Comment

by:BCHCAdmin
ID: 40486139
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?
0
 
LVL 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 500 total points
ID: 40486299
>> 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:
if %ERRORLEVEL% NEQ 0 (
:: put your powershell mail command here
) else (
echo "Everything OK"
)
0
 

Author Closing Comment

by:BCHCAdmin
ID: 40494896
Ended up using the PowerShell Script to open and read the file but thank you for getting me in the right direction!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question