Search code examples
powershellautomationftpbatch-processingwinscp

Automated FTP transfer using WinSCP and Batch file with Email notification


I am trying to write a batch file that will:

  • connect to FTP server using WinSCP
  • use RSA keys to authenticate
  • try two different keys, in case one fails
  • send an email notification about fail transfers
  • save logs

Solution

  • Solution to the question:

    @echo off
    
    :: FTP Configuration
    SET TRIES=2
    SET INTERVAL=10
    SET [email protected]
    SET HostKey=
    SET ProdHostKey=ssh-rsa 2048 KeyKeyKeyKeyKeyKeyKeyKey
    SET DRHostKey=ssh-ed25519 256 KeyKeyKeyKeyKeyKeyKeyKey
    SET PrivateKey=C:\RSA_Privatekey\Key.ppk
    SET Source=/Files/Work/
    SET Destination=D:\Inbox\
    
    :: Log File Configuration
    SET DateStamp=%date:~-4,4%%date:~-10,2%%date:~-7,2%
    SET LogFile=D:\Logs\WinSCP\WinSCP-FTP_%DateStamp%.log
    
    :: Email Configuration
    SET SMTPServer=smtp.server.com
    SET EmailFrom=%computername%@server.com
    SET EmailTo="[email protected]", "[email protected]"
    SET EmailSubject="FTP Transfer Failure"
    SET EmailAttachment="%LogFile%"
    SET EmailBody="FTP transfer failed.`r`nLog file attached."
    
    :Begin
    SET HostKey=%ProdHostKey%
    
    :Retry
    :: Launch WinSCP
    "C:\Program Files (x86)\WinSCP\WinSCP.com" ^
        /log="%LogFile%" /ini=nul ^
        /command ^
        "Option confirm off" ^
        "open sftp://%Server%/ -hostkey=""%HostKey%"" -privatekey=""%PrivateKey%"" -rawsettings ConsiderDST=0" ^
        "Get -delete "%Source%*.*" "%Destination%"" ^
        "Close" ^
        "Exit"
    
    SET WINSCP_RESULT=%ERRORLEVEL%
    if %WINSCP_RESULT% equ 0 (
        echo Success
        ) else (
                SET /A TRIES=%TRIES%-1
                IF %TRIES% GTR 1 (
                    echo Transfer failed, retrying using DR hostkey, in %INTERVAL% seconds...
                    timeout /t %INTERVAL%
                    SET HostKey=%DRHostKey%
                    goto Retry
                ) else (
                        echo Connection failed, aborting and sending an email notification.
                        echo.
                        echo From: %EmailFrom%
                        echo To: %EmailTo%
                        echo Subject: %EmailSubject%
                        echo Attachments: %EmailAttachment%
                        echo Message: %EmailBody%
                        powershell.exe -NoLogo -NoProfile -Command ^
                            "Send-MailMessage" ^
                                "-To %EmailTo%" ^
                                "-From '%EmailFrom%'" ^
                                "-Subject '%EmailSubject%'" ^
                                "-SmtpServer '%SMTPServer%'" ^
                                "-Body "%EmailBody%"" ^
                                "-Attachments %EmailAttachment%"
                                
                        exit /b 1
                        )
    )
    
    exit /b %WINSCP_RESULT%