Search code examples
powershelljenkinssftpwinscpwinscp-net

Build in Jenkins succeeds even when WinSCP PowerShell transfer script fails


I sometimes noticed that some Jenkins builds have an error in their output without ending with an error (for instance, with an exit 1) but with a success.

This comes from the WinSCP $transferResult.Check() function.

For instance, with one of our scripts that upload CSV files to a SFTP Remote directory: enter image description here

Would it be possible to add a condition to this function, that ends with an exit 1 if there is an error in the $transferResult.Check()?

This way, it cans prevent Jenkins to end with a successful build.

My PowerShell file

# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Ftp
    HostName = ""
    UserName = ""
    Password = ""
    Timeout = New-TimeSpan -Seconds 300
}

# Set local directories
$LocalDir = "\\localdir\*"
$RemoteDir = "/remotedir/"

$session = New-Object WinSCP.Session

try
{
    # Check if the local directory exists
    if (Test-Path -Path $LocalDir) {
        "Local directory $LocalDir exists! All good."
    } else {
        "Error: Local directory $LocalDir doesn't exist. Aborting"
        exit 1
    }

    # Check if the local directory contain files
    if((Get-ChildItem $LocalDir | Measure-Object).Count -eq 0)
    {
        "Local directory $LocalDir has currently no CSV files. Stopping script."
        exit 0
    } else {
        "Local $LocalDir contains CSV files. Starting now SFTP Upload Session..."
    }

    # Connect to the FTP server

    $session.Open($sessionOptions)
    $session.Timeout = New-TimeSpan -Seconds 300

    # Upload the files
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Automatic
    $transferOptions.FileMask = "*.csv, |*/";

    $transferResult = $session.PutFiles($LocalDir, $RemoteDir, $true, $transferOptions)
    
    # Throw on any error
    $transferResult.Check()

    # Print results
    foreach ($transfer in $transferResult.Transfers)
    {
        Write-Host -ForegroundColor green "Upload of $($transfer.FileName) to remote SFTP directory $RemoteDir succeeded."
    }
    
    Write-Host "$($transferResult.Transfers.Count) file(s) in total have been transferred."
}
finally
{
    $session.Dispose()
}
exit 0
catch 
{
    Write-Host -ForegroundColor red "Error: $($_.Exception.Message)"
    exit 1
}

Solution

  • Your try block has invalid syntax. The catch never happens as the exit 0 unconditionally aborts your script before. And were not for the exit 0, the script would fail on the catch, as the catch must come before the finally.

    See https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally

    The correct syntax is:

    $session = New-Object WinSCP.Session
    
    try
    {
        # Your code
    }
    catch 
    {
        Write-Host -ForegroundColor red "Error: $($_.Exception.Message)"
        exit 1
    }
    finally
    {
        $session.Dispose()
    }
    
    exit 0