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