Solved

powershell sync local folder with SFTP folder

Posted on 2016-11-24
7
39 Views
Last Modified: 2016-11-28
hello experts
i need a powershell configure to windows schedule task to run automatically, which synchronize local folder with SFTP, but i don't have the ability write it by myself, i did search on google, but all is about synchronize between folder.
so could you show me an example about this or write one for me in this case?

thank you
0
Comment
Question by:beardog1113
  • 4
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
Michal Ziemba earned 500 total points
ID: 41901309
Hi,
There isn't currently a built-in PowerShell method for handling the SFTP. You'll have to use something like WinSCP.
First build the WinSCP script and than schedule the WinSCP with this script as a parameter.
Here is an example of the WinSCD script (let's call it synchronize.txt):

# Connect to SFTP server using a password
open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
# Synchronize folders
synchronize both "C:\local folder" "/home/remote_folder"
# Exit WinSCP
exit

Use the synchronize local to apply changes in the remote directory to the local directory.
Use  the synchronize remote to apply changes in the local directory to the remote directory.
Use the synchronize both to synchronize local and remote directories reciprocally.

If you really want to put this into Powershell, just crate a new script file and put the following command to run the WinSCP with the synchronization.txt script:
& 'C:\Program Files (x86)\WinSCP\WinSCP.exe' ".\synchronize.txt"

Hope this helped.
Regards
Mike
0
 

Author Comment

by:beardog1113
ID: 41903568
hello
i tried your instruction to setup this, the screenshot is the configuration of windows schedule task, could you tell me this is correct or not? this task result is always under "directory name individual"
Untitled.pngand below is the content of synchronize.txt
# Connect to SFTP server using a password
open sftp://mengfan:fanmeng@10.137.8.52/ -hostkey="ssh-rsa 1024 eb:54:09:db:54:ac:ce:9b:07:d4:d2:43:dd:49:74:f5"
# Synchronize folders
synchronize both "C:\folder1" "/home/folder1"
# Exit WinSCP
exit

could you let me know if there something need to modify? /home is the path of SFTP root directory or something need to replace?

thank you
0
 
LVL 3

Expert Comment

by:Michal Ziemba
ID: 41904052
Hi,
Put the script name used as a parameter into the "Add arguments (optional)" field
11.png
Let me know if this worked.
/Mike
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:beardog1113
ID: 41905017
hi Mike
tried this but while run the schedule task manually get the following error
Untitled.pngbut when i using CMD to run it, there is not problem, see below please
Untitled1.png
btw, content in synchronize.txt is below now:
# Connect to SFTP server using a password
open sftp://mengfan:fanmeng@10.137.8.52/ -hostkey="ssh-rsa 1024 eb:54:09:db:54:ac:ce:9b:07:d4:d2:43:dd:49:74:f5"
# Synchronize folders
synchronize both "C:\folder1" "/folder1"
# Exit WinSCP
exit

so any idea how to fix in schedule task?

thank you
0
 

Author Comment

by:beardog1113
ID: 41905029
hi Mike
i get it fixed which put /script=c:\synchronize.txt into "Add arguments (optional)" field

thank you very much
0
 
LVL 3

Expert Comment

by:Michal Ziemba
ID: 41905163
Hi,
You arę right. Good that you made it.
0
 

Author Closing Comment

by:beardog1113
ID: 41905233
thank you
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

19 Experts available now in Live!

Get 1:1 Help Now