Hello martin,
Initial I've a cmd file:
Cd D:\SFTP
date /t > \\IP-ADRESS\d$\SFTP_Bloomberg_BTCA\SFTP.txt
time /t >> \\IP-ADRESS\d$\SFTP\SFTP.txt
powershell.exe "\\IP-ADRESS\d$\SFTP\sftp.ps1" >> \\IP-ADRESS\d$\SFTP\SFTP.txt
move \\IP-ADRESS\d$\SFTP\PROD\*.csv \\IP-ADRESS\d$\SFTP\PROD\archive\ >> \\IP-ADRESS\d$\SFTP\SFTP.txt
del /f \\IP-ADRESS\d$\SFTP\PROD\*.csv >> \\IP-ADRESS\d$\SFTP\SFTP.txt
date /t >> \\IP-ADRESS\d$\SFTP\SFTP.txt
time /t >> \\IP-ADRESS\d$\SFTP\SFTP.txt
The PowerShell launched is formatted like that,
try
{
# Load WinSCP .NET assembly
Add-Type -Path "D:\SFTP\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "url"
UserName = "user"
SshPrivateKeyPath = "D:\SFTP\private.ppk"
PrivateKeyPassphrase = "pass"
GiveUpSecurityAndAcceptAnySshHostKey = "true"
}
$session = New-Object WinSCP.Session
try
{
$session.SessionLogPath = "D:\SFTP\winscp-upload.log"
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$transferOptions.FilePermissions = $Null
$transferOptions.PreserveTimestamp = $False
$transferResult = $session.PutFiles("D:\SFTP\prod\*.csv", "/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
I've some instability on the network and get the timeout when I try to connect
It would like that
"Error: Network error: Connection to "*******-******.com" timed out. " in a txt file,
I'll user it has flag file to retry the connection if it's failed.