Solved

WinSCP Scripting Problem

Posted on 2014-12-04
4
261 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
  • 2
  • 2
4 Comments
 
LVL 37

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 37

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I do this in Pyhton? 12 126
AWS CLI - Instances, Volumes & Tagging 2 95
Example unit tests with AngularJS 3 46
Powershell Script need assistance to make some changes 4 82
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

832 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