Output file when error

Advertisement

npoinard
Joined:
Posts:
2

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.

Reply with quote

Advertisement

npoinard
Joined:
Posts:
2

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.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

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
}

Reply with quote

Advertisement

You can post new topics in this forum