Topic "SFTP Download with WinSCP and PowerShell - just to show"

Author Message
danis
[View user's profile]

Joined: 2012-02-20
Posts: 8
Hello all,
I'd like to show you my short script for downloading files via SFTP from a Server to a local machine.

It is working good now, but I am new to PowerShell and WinSCP (saw it the first time last week..) - so maybe you got some ideas to make it better..

Code:
#FTP Connection Parameters
$username = "yourUserName"
$password = ""
[bool]$usepassword = $false #false if PPK File used, otherwise true
$hostkey = "ssh-rsa 1024 00:00:00:00:00:00:00:00:00:00:00:00:00:00:0:00" #fingerprint of the FTP Server
$WinSCPPath = "C:\Program Files (x86)\WinSCP\"
$ppkpath = "H:\Scripts\private_key.ppk"
$UserFolderFTP_from = "/from"
$UserFolderFTP_to = "to"
$server = "ServerHere"

$LocalFolder_Temp = "H:\Scripts\Temp\"
$logFolder = "H:\Logs\"
$logfile = "FTPLog.txt"

function DownloadFiles
   {
      $date = Get-Date -format "yyyy-MM-dd HH:mm:ss.000"
      out-file $logFolder$logfile -append   -encoding "ASCII" -InputObject ". $date - Download started"
      #Create Command for WinSCP to open Connection.
      $Script = '"option batch abort" '
        $Script += ' "option confirm off" '
      if ($usepassword)
         {
         $Script += ' "open sftp://' + "$username:$password@$server -hostkey=" + '""' + $hostkey + '"""'
         }
      else
         {
         $Script += ' "open sftp://' + "$username@$server -privatekey=$ppkpath -hostkey=" + '""' + $hostkey + '"""'
         }
        $Script += ' "cd ' + $UserFolderFTP_from + '"'
      $Script += ' "option transfer binary"'       
        $Script += ' "get  -delete * ' + $LocalFolder_Temp + '"'       
        $Script += " close"
        $Script += " exit"
      
      Set-Location -Path $WinSCPPath
      .\WinSCP.com /log=$logFolder$logfile /command $Script
      $date = Get-Date -format "yyyy-MM-dd HH:mm:ss.000"
      out-file $logFolder$logfile -append   -encoding "ASCII" -InputObject ". $date - Download Ended"
   }
   
function UploadFiles
   {
      $date = Get-Date -format "yyyy-MM-dd HH:mm:ss.000"
      out-file $logFolder$logfile -append -encoding "ASCII"   -InputObject ". $date - Upload started"
      #Create Command for WinSCP to open Connection.
      $Script = '"option batch abort"'
        $Script += ' "option confirm off"'
        if ($usepassword)
         {
         $Script += ' "open sftp://' + "$username:$password@$server -hostkey=" + '""' + $hostkey + '"""'
         }
      else
         {
         $Script += ' "open sftp://' + "$username@$server -privatekey=$ppkpath -hostkey=" + '""' + $hostkey + '"""'
         }
        $Script += ' "cd ' + $UserFolderFTP_to + '"'
      $Script += ' "lcd ' + $LocalFolder_TransDat + '"'
      $Script += ' "option transfer binary"'       
      $Script += ' "put -delete * -nopermissions -nopreservetime"'
        $Script += " close"
        $Script += " exit"
      
      Set-Location -Path $WinSCPPath
      .\WinSCP.com /log=$logFolder$logfile /command $Script
      $date = Get-Date -format "yyyy-MM-dd HH:mm:ss.000"
      out-file $logFolder$logfile -append   -encoding "ASCII" -InputObject ". $date - Upload Ended"
   }
   
DownloadFiles
UploadFiles


Regards,
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
We have just released WinSCP .NET assembly which may make your script way simpler.
https://winscp.net/eng/docs/library
We plan to add a powershell example later.
_________________
Martin Prikryl
danis
[View user's profile]

Joined: 2012-02-20
Posts: 8
Hi Prikryl,
I just wanted to Download the .NET assembly.


It is written:

Quote:
WinSCP .NET assembly is available in a separate package named winscpXXXautomation.zip on WinSCP download page. Follow the .NET assembly/COM library link.


In the Download section I can't find it anywhere.. could you help me?
Petr
[View user's profile]
Moderator
Joined: 2006-01-26
Posts: 50
Please look at WinSCP 5.0.6 beta, it is called .NET assembly / COM library.
clt

Guest


I beg you to add powershell example. Thank you!
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Basic PowerShell example added
https://winscp.net/eng/docs/library#powershell
More examples (and syntax highlighting) coming soon.
_________________
Martin Prikryl
Jonboy

Guest


Petr wrote:
Please look at WinSCP 5.0.6 beta, it is called .NET assembly / COM library.



I cannot find any WinSCP 5.0.6. beta nor can I find the winscpXXXautomation.zip Please folks let us get or act together.
Petr
[View user's profile]
Moderator
Joined: 2006-01-26
Posts: 50
Please look at WinSCP Download page, section WinSCP 5.0.6 beta, it is called .NET assembly / COM library.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License