Solved

powershell sync local folder with SFTP folder

Posted on 2016-11-24
7
120 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  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

Synchronize a new Active Directory domain with an existing Office 365 tenant
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

615 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