Uploading and Downloading files from Secure FTP

Hi All,

I have shared two methods in the past in relation to FTP, first one is for uploading files to FTP http://msexchange.me/2014/03/05/upload-files-to-ftp-server/

and second one for downloading files from Secure FTP & monitoring it.

http://msexchange.me/2014/11/21/monitoring-secure-ftp-file-uploads/

As we have to use some secure FTP client so I have used Curl http://curl.haxx.se/ in this case, but today we will use winscp which is the most popular client out there.

I have updated the old script on Microsoft Script gallery as well to include  winscp method.

https://gallery.technet.microsoft.com/scriptcenter/Secure-FTP-file-Uploads-f33bbda7

Now lets look at the powershell script to upload the file to secure FTP.

Extract the zip file from below link :

https://gallery.technet.microsoft.com/scriptcenter/Uploading-and-Downloading-c0e66289

  • FileV3Upload is the folder which contains FileV3.csv file that will be uploaded to secureftp if the file is of today’s time-stamp.

We have used this script to upload the file which is daily copied to this folder & if the new file is not arrived than it will not be uploaded.

  • Command1.txt contains secureftp details to upload the files (please alter it accordingly)

# Automatically abort script on errors

option batch abort
# Disable overwrite confirmations that conflict with the previous

option confirm off
# Connect

open sftp://user:password@xxx.xxx.xxx.xxx
# Upload the file to current working directory

put .\FileV3Upload\FileV3.csv -nopreservetime -nopermissions
# Disconnect

close
# Exit WinSCP

exit

  • Update FileV3-Upload.ps1 with smtp details.

$smtpServer = “smtpserver”

$fromadd = “DoNotReply@labtest.com”

$email1 = “VikasSukhija@labtest.com”

Now you just have to schedule the batch file & daily file will get uploaded to secure FTP.

Please rate the contribution if it works for you 🙂

###################################################################### 
###################################################################### 
#               Author: Vikas Sukhija 
#               Date:- 12/22/2014 
#        Modified:- 07/23/2015 (updated to use WINSCP) 
#               Description:- This script will upload the file to sftp 
#                
###################################################################### 
######upload file from sftp########################################## 
 
$date = get-date -format d 
$date = $date.ToString().Replace(“/”, “-”) 
$time = get-date -format t 
$time = $time.ToString().Replace(":""-"$time = $time.ToString().Replace(" """) 
 
$logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt" 
 
$smtpServer = "smtpserver" 
$fromadd = "DoNotReply@labtest.com" 
$email1 = "VikasSukhija@labtest.com" 
 
$date1=Get-Date -Hour 0 -Minute 0 -Second 0 
 
Start-Transcript -Path $logs  
 
 
 
$lwrt=Get-ChildItem .\FileV3Upload\FileV3.csv 
 
if ($error -ne $null) 
      { 
#SMTP Relay address 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1) 
 
$msg.Subject = "Ftp file Monitoring script error" 
$msg.Body = $error 
$smtp.Send($msgexit 
       } 
 
if($lwrt.LastWriteTime -ge "$date1") 
{ 
Write-host "File for Today found and will be uploaded to sftp" -ForegroundColor green 
 
.\winscp556\WinSCP.com /script=command1.txt 
 
} 
else 
{ 
write-host "Today's date file not found on share" -ForegroundColor yellow 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1) 
 
$msg.Subject = "Today's date file not found on share" 
$msg.Body = "Today's date file not found on share" 
$smtp.Send($msg) 
 
} 
 
######################################################################## 
if ($error -ne $null) 
      { 
#SMTP Relay address 
$msg = new-object Net.Mail.MailMessage 
$smtp = new-object Net.Mail.SmtpClient($smtpServer#Mail sender 
$msg.From = $fromadd 
#mail recipient 
$msg.To.Add($email1) 
 
$msg.Subject = "Ftp file Monitoring script error" 
$msg.Body = $error 
$smtp.Send($msg) 
       } 
 
Stop-Transcript 
 
 
#############################################################################

Regards

Sukhija Vikas

http://msexchange.me

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s