6

I have some simple PowerShell scripts that I have made into executables with Bamboo, by adding a path such as C:\build-scripts\bamboo-build-scripts\clear-directory.ps1 as the path for the executable for a new capacity in Bamboo.

However, several scripts, even ones that execute correctly in the build process when they are made as a "script" process, will fail when they are run in this way, by giving the return code -1. Here is an example from the build log:

simple  18-Jun-2015 13:14:06    Failing task since return code of [C:\build-scripts\bamboo-build-scripts\update-checker.ps1 GeometryClassLibrary] was -1 while expected 0

This occurs with multiple PowerShell scripts, and causes the rest of the build procecss to fail.

Here is an example PowerShell script, which I execute by passing the argument to a directory:

Remove-Item $args[0] -Force -Recurse
[io.directory]::CreateDirectory($args[0])

Is there something I need to add to the PowerShell script to make it exit with the correct code? Or am I not defining the executable properly in Bamboo?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Jake
  • 3,411
  • 4
  • 21
  • 37
  • Bamboo's support for powershell has seemed iffy to me in my limited use of it as script tasks. – Etan Reisner Jun 18 '15 at 18:30
  • We've had relatively good success using it in script tasks, but as some of our scripts had gotten more complex, it seemed to make sense to move to a separate executable and pass arguments. That's where the problem has arisen: the scripts were working fine before as script tasks. – Jake Jun 18 '15 at 18:32
  • I've seen it ignore errors (thrown errors that get shown in the log) and keep going during script execution before. Adding strict mode and manually setting EA has helped a bit with that I think though. – Etan Reisner Jun 18 '15 at 18:33
  • Trying https://marketplace.atlassian.com/plugins/com.stellarity.bamboo.powershell-task might be of use for you also. – Etan Reisner Jun 18 '15 at 19:23
  • @EtanReisner Thanks for the suggestion, but we're using Cloud, where that plug-in is not supported. – Jake Jun 18 '15 at 20:30

2 Answers2

0

You can try a few things:

Execution Policy

It could be that the scripts aren't executing at all, perhaps because the policy is set not to execute them. Try invoking powershell.exe directly:

powershell.exe -ExecutionPolicy Bypass -File C:\build-scripts\bamboo-build-scripts\clear-directory.ps1

(see this answer for more switches)

Piping

NSClient++ used to have issues with invoking checks written in powershell due to problems with the exit code. Their solution looked like this:

cmd /c echo C:\build-scripts\bamboo-build-scripts\clear-directory.ps1; exit $LastExitCode | powershell.exe -Command -

Maybe that will give a more accurate code.

Community
  • 1
  • 1
briantist
  • 45,546
  • 6
  • 82
  • 127
  • Here is an example of how I am calling the script: http://i.imgur.com/wAUXJ3D.png How would I modify this executable to reflect running it in this manner? – Jake Jun 18 '15 at 19:46
  • @Jake Can you just copy and paste what I posted into that box? I haven't used Bamboo, so I'm not sure if it takes the parameters and such. – briantist Jun 18 '15 at 19:47
  • 1
    The second works from the command prompt, but pasting the whole thing into Bamboo as an executable does not work. It reports `Unable to find executable at cmd /c echo C:\build-scripts\bamboo-build-scripts\clear-directory.ps1; exit $LastExitCode | powershell.exe -Command C:\build-scripts\bamboo-build-scripts\clear-directory.ps1. Will try to run it anyway.` If someone with Bamboo-specific knowledge could chime in, that would be great. – Jake Jun 18 '15 at 20:15
  • I imagine that executable wants to be an executable and the arguments would need to come from the task. You could try that for this. But at that point I'm not sure that's much more useful than just creating a powershell task that you pass the script to. – Etan Reisner Jun 18 '15 at 21:47
0

I would recommend a few changes to your scripts to give better output to the Bamboo logs while they run to help narrow down the problem.

Put some simple output lines in so that you know the powershell script ran, and how far it made it

Write-Host "Running script blah"
Remove-Item $args[0] -Force -Recurse
Write-Host "Directories removed"
[io.directory]::CreateDirectory($args[0])
Write-Host "Directory $args[0] created"

Second, wrap everything in a try catch and write any exceptions

try {
Remove-Item $args[0] -Force -Recurse
[io.directory]::CreateDirectory($args[0])
{
catch {
Write-Host $_.Exception.GetType().FullName, $_.Exception.Message
}

As far as your script, you didn't post how you are calling it from Bamboo. I do most of my scripts inline, but I do have a couple that I've saved as .ps1 and uploaded with the repository and call with arguments. I'm not sure if the args[x] method would work for pulling args, but the suggested method that I followed was declaring params.

param(
[string]$SomeString
)
Write-Host "Param value: $SomeString"

Then call it from Bamboo as

SomeScript.ps1 -SomeString %BAMBOO_SOME_VARIABLE%
dgates82
  • 398
  • 3
  • 8