Powershell scripts running on Bamboo don’t return the correct exit code


As part of an deployment project on Bamboo CI, I was running a powershell script to deploy an ASP.NET application to a Cloud Service on Azure.

Even though there was an error executing the script, Bamboo was setting the status of the Deployment to Success. Why? Because the exit code returned by the powershell script is always 0 (zero means successful execution).

After some research I was able to find a way to return the correct exit code in case of failure. I added the following lines to the top of my powershell script:

trap
{
    write-output $_
    exit 1
}

The trap statement includes a list of statements to run when a terminating error occurs – in this case, every time an error occurs the error message will be displayed and then the script will return a correct exit code indicating a failure. I am returning 1 but any value different from 0 (zero) will do the trick 🙂

Advertisements

2 thoughts on “Powershell scripts running on Bamboo don’t return the correct exit code

  1. Hi. I tried this but it doesn’t seem to work with my script. Is my script correct? Thanks!

    trap
    {
    write-output $_
    exit 1
    }

    Add-PSSnapin VMware.VimAutomation.Core
    Add-PSSnapin VMware.VimAutomation.Vds
    if(get-item HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\VMware.VimAutomation.Core){
    . ((get-item HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\VMware.VimAutomation.Core).GetValue(“ApplicationBase”)+”\Scripts\Initialize-PowerCLIEnvironment.ps1″)
    }
    else
    {
    write-warning “PowerCLI Path not found in registry, please set path to Initialize-PowerCLIEnvironment.ps1 manually. Is PowerCli aleady installed?”
    . “D:\Programs (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1″
    }

    # Connect to vCenter
    Write-Host -NoNewline ” Connecting to vCenter…”
    Connect-VIServer ${bamboo.sVCenterHost} ${bamboo.sVCenterUser} ${bamboo.sVCenterPassword} -ErrorAction SilentlyContinue -WarningAction SilentlyContinue |out-null
    if(!$?){
    Write-Host -ForegroundColor Red ” Could not connect to $sVCenterHost”
    exit 2
    }
    else{
    Write-Host “OK”
    }

    Write-Host “Reverting VM”
    Set-VM -VM ${bamboo.sVCenterVM} -Snapshot ${bamboo.sVCenterVMSnapshot} -Confirm:$false
    if(!$?){
    exit $LASTEXITCODE
    }
    else{
    Write-Host “Revert VM Complete”
    }

    • Hi Dom,

      I’m not a Powershell expert but I think that if you want Bamboo to catch the error then you shouldn’t use this in your script:

      -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

      Rui

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s