Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

If you need to retry the connection, just loop, when the exception is thrown.

If you insist on your error file hack, just create the file in the catch clause.
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    Set-Content -Path "error.txt" -Value $_.Exception.Message
    exit 1
}
npoinard

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

Output file when error

Hello,
sorry if my question has been already asked, I haven't found it.
I've in my SFTP script, an time out error
Error: Network error: Connection to "*******-******.com" timed out.

I would like to have an .txt file when I got this error.
Then I could make a check on the présence of this file.

Thanks in advance
Nicolas.