Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

powershell sync local folder with SFTP folder

Posted on 2016-11-24
7
Medium Priority
?
251 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 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

721 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