Solved

WinSCP Scripting Problem

Posted on 2014-12-04
4
239 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 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
Gerwin Jansen 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now