SFTP Download with WinSCP and PowerShell - just to show

Advertisement

danis
Joined:
Posts:
8

SFTP Download with WinSCP and PowerShell - just to show

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..

#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,

Reply with quote

Advertisement

danis
Joined:
Posts:
8

Hi Prikryl,
I just wanted to Download the .NET assembly.


It is written:

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?

Reply with quote

Advertisement

Jonboy
Guest

What???? cant find your stuff

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.

Reply with quote

Advertisement

You can post new topics in this forum