Solved

powershell sync local folder with SFTP folder

Posted on 2016-11-24
7
17 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:beardog1113
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,
You arę right. Good that you made it.
0
 

Author Closing Comment

by:beardog1113
Comment Utility
thank you
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

10 Experts available now in Live!

Get 1:1 Help Now