Please look at WinSCP Download page, section WinSCP 5.0.6 beta, it is called .NET assembly / COM library.
- Petr
Please look at WinSCP 5.0.6 beta, it is called .NET assembly / COM library.
WinSCP .NET assembly is available in a separate package named winscpXXXautomation.zip on WinSCP download page. Follow the .NET assembly/COM library link.
#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