Beginner needs help making simple script to copy via SFTP

Advertisement

danbrown1888
Joined:
Posts:
4
Location:
NJ

Beginner needs help making simple script to copy via SFTP

Hello scripting experts! I'm new to scripting and found this excellent tool today. I've been asked to automate the copying of two files from an SFTP site. I read through the documentation but the syntax is throwing me off a bit. Here is what I have so far:

winscp.com /command "option batch abort" "option confirm off" "open securesite.net" "get examplefile.txt" "exit"

Close but not quite. I also need to pass credentials to authenticate then I need to make sure the files are copied to a specific directory. Could anyone fill in the blanks for me?

Reply with quote

Advertisement

danbrown1888
Joined:
Posts:
4
Location:
NJ

Figured it out, here is what I did:

.bat file set as scheduled task to call powershell.exe c:\scripts\script.ps1

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

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "ADDRESS.net"
$sessionOptions.UserName = "USERNAME"
$sessionOptions.Password = "PASSWORD"
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 XX:XX:XX:XX:XX:XX:XX etc"

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

$localPath = "C:\PATH"
$remotePath = "/Inbox/"
$file = "FILE.bin"

# Format timestamp
$stamp = $(Get-Date -f "yyyyMMddHHmmss")

# Download the file and throw on any error
$session.GetFiles(
($remotePath + $file),
($localPath + $stamp + "." + $file)).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

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

Reply with quote

umcans
Joined:
Posts:
2
Location:
Colombia

Good afternoon all, I am a Beginner on making scripts to copy via SFTP, i found this article with an explanation on what i need, sorry for my unawareness on this kind of process. It is possible for you to help me and clarify a little be more on how i can create a simple script that allow me to connect as an automatic task using a batch file that can do the following task>

* Open the WinSCP on a specific date and time
* Select one of the SFTP accounts that i have configure on my WinSCP client (WinSCP.exe)
example (username@hostname)
* Authenticate the user account (username@hostname) and password (*********)
* Select a specific path where the files are upload it (this folder is encrypted by the connection key that we create to setup the connection -xxxxpriv.ppk-)
C:\ftproot\folder\Live\in
* Copy the files on this location and save it in to a different local path on the server
C:\Users\username\Desktop\Temp
* Save a log of what files was copied and the time that this task was executed
* Disconnect of the running session


More and less this is what i need and i see on the structure of the script that you refer on your message but as i said before it is not clear for me because i`m not familiar with this process.


Thanks in advance for your help and support.
Best regards,


Angel

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,449
Location:
Prague, Czechia

umcans wrote:

Good afternoon all, I am a Beginner on making scripts to copy via SFTP, i found this article with an explanation on what i need, sorry for my unawareness on this kind of process. It is possible for you to help me and clarify a little be more on how i can create a simple script that allow me to connect as an automatic task using a batch file that can do the following task>
We do not provide script writing service here. Please try yourself and ask specific questions on problems you encounter.

Reply with quote

umcans
Joined:
Posts:
2
Location:
Colombia

Thanks!

martin wrote:

umcans wrote:

Good afternoon all, I am a Beginner on making scripts to copy via SFTP, i found this article with an explanation on what i need, sorry for my unawareness on this kind of process. It is possible for you to help me and clarify a little be more on how i can create a simple script that allow me to connect as an automatic task using a batch file that can do the following task>
We do not provide script writing service here. Please try yourself and ask specific questions on problems you encounter.


Hi, thanks for your advice, i was able to complete the Script and i was trying to get help from the experts on the software or any recommendation.

Reply with quote

Advertisement

You can post new topics in this forum