Solved

powershell sync local folder with SFTP folder

Posted on 2016-11-24
7
31 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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
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 …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

15 Experts available now in Live!

Get 1:1 Help Now