We were asked to set up an automated upload from one of our servers to an SFTP site. There will be a file that is exported from a database to a filer every Monday morning and they want the file to be uploaded to SFTP on Tuesday. The current authentication method we are using is username and password (I believe there was an option to have key file as well but username/password option was chosen).
The way I am envisioning this is to have a script sitting on a server that will be triggered by Windows Task scheduler to run at a specific time (Tuesday) that will grab the file in question upload it to the SFTP and then move it to a different location for backup purposes.
For example:
Local Directory: C:\FileDump
SFTP Directory: /Outbox/
Backup Directory: C:\Backup
I tried few things at this point WinSCP being one of them as well as SFTP PowerShell Snap-In but nothing has worked for me so far.
This will be running on Windows Server 2012R2.
When I run Get-Host
my console host version is 4.0.
Thanks.
There isn't currently a built-in PowerShell method for doing the SFTP part. You'll have to use something like psftp.exe or a PowerShell module like Posh-SSH. Here is an example using Posh-SSH:
I'm fairly new to SFTP and PowerShell as well, and I need help downloading files from an SFTP server onto my application server. I believe that the Posh-SSH module can help with establishing an SFTP connection, but most code I have seen until now involves uploading files onto the SFTP server, not downloading from... would appreciate any advice!
The input here must have USER [USERNAME] [PASS] on a single line after the OPEN [HOSTNAME], per the previous comment. The most trivial way to upload a binary file to an FTP server using PowerShell is using WebClient.UploadFile:
To download files, you must first set up a session. The session is a persistent connection you’ll use to perform various SFTP operations. To set up a session, provide a PSCredential object via Get-Credential and specify the remote SFTP server you’d like to connect to using the New-SFTPSession command.
You didn't tell us what particular problem do you have with the WinSCP, so I can really only repeat what's in WinSCP documentation.
Download WinSCP .NET assembly.
The latest package as of now is WinSCP-5.19.6-Automation.zip
;
Extract the .zip
archive along your script;
Use a code like this (based on the official PowerShell upload example):
# Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "example.com" UserName = "user" Password = "mypassword" SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...=" } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Upload $session.PutFiles("C:\FileDump\export.txt", "/Outbox/").Check() } finally { # Disconnect, clean up $session.Dispose() }
You can have WinSCP generate the PowerShell script for the upload for you:
You will get a code like above with all session and transfer settings filled in.
(I'm the author of WinSCP)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With