Powershell script as executable giving "wrong" giving return code -1 in Bamboo Powershell script as executable giving "wrong" giving return code -1 in Bamboo powershell powershell

Powershell script as executable giving "wrong" giving return code -1 in Bamboo


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.


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 -RecurseWrite-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%