Powershell Scripting – Automatic upload files to remote FTP

Yesterday we had a needed, we needed to transfer every n minutes all the xml files located in a folder to a remote ftp location. There’s a lot of ways to do this and some of them include a payment softwares. In our case files were xml created by a SAP server in a Windows enviroment, for this reason we bet for a powershell script solution that we can call using Windows task scheduler

This script uses the WinSCP dll libraries, for this reason first of all we are going to install this light and free software

You’ll find here: https://winscp.net/eng/download.php

How the script works.

1.- The script, moves files from existing folder to a remote ftp site.

2.- After file is uploaded correctly, this  will be moved to a Success folder

3.- If file exists in Ftp or Success folder, this it will be overwrited

4.- If error occurs, this are going to print in an output log file

5.- All this movements with timestamp  will be written in a log

And this is the script that you have to put ins a ps1 file, for example upload_ftp.ps1

#Preparing log
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
#Log location file
Start-Transcript -path C:\ftpscript\log\log.txt -append

	# Load WinSCP .NET assembly 
	[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCPnet.dll") | Out-Null 

	# Set FTP data
	$sessionOptions = New-Object WinSCP.SessionOptions 
	$sessionOptions.Protocol = [WinSCP.Protocol]::ftp 
	$sessionOptions.HostName = "ftp.remotelocation.com" 
	$sessionOptions.UserName = "ftpusername" 
	$sessionOptions.Password = "ftpmpassword" 
	# $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" 

	$session = New-Object WinSCP.Session 

			# Connect 

			$localPath = "C:\ftpscript\upload\*.xml" 
			$remotePath = "/" 
			$backupPath = "C:\ftpscript\uploaded" 
			$aData = Get-Date

			# Upload files, collect results 

			$transferOptions = New-Object WinSCP.TransferOptions 
			$transferResult = $session.PutFiles($localPath, $remotePath) 

			# Files itineration
			foreach ($transfer in $transferResult.Transfers) 
					# Correct o error? 
					if ($transfer.Error -eq $Null) 
							Write-Host ("${aData} - File {0} was uploaded and moved to backup folder" -f 
							# If, Ok move to backup folder
							Move-Item $transfer.FileName $backupPath -force 
							Write-Host ("${aData} - File {0} with error: {1}" -f 
							$transfer.FileName, $transfer.Error.Message) 
				# Disconnect and clean

			exit 0 

catch [Exception] 
		Write-Host $_.Exception.Message 
		exit 1 

# Stop log writing

3 Replies to “Powershell Scripting – Automatic upload files to remote FTP”

  1. Hi , Thank you for this it works perfectly fine. Can you please suggest if I want to use public key instead of password , how to do that ?

  2. Hi
    I m trying run this script but i am not seeing any log or file transfers. I thiink , any software to be installed before running this script. I already installed winscp. Please let me know.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.