Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Bamboo Powershell script always fails with "Failing task since return code of XXX was -1 while expected 0"

David Miller March 14, 2016

Hello,

I have created a simple Bamboo Powershell task (Bamboo cloud) and for the life of me cannot get anything to run successfully.

Right now I've disabled all other Tasks in the Plan and have my Powershell script down to this single line...

Write-Host "Test"

I'm using a single "Remote" Build Agent that is running on my local network.

The output from my build is this...

simple	14-Mar-2016 17:41:46	Build CT - User Security - Default Job #14 (CT-US-JOB1-14) started building on agent DEV-BUILD01.criticaltech.com
simple	14-Mar-2016 17:41:46	Remote agent on host DEV-BUILD01.criticaltech.com
simple	14-Mar-2016 17:41:46	Build working directory is C:\Users\dmiller\bamboo-agent-home\xml-data\build-dir\CT-US-JOB1
simple	14-Mar-2016 17:41:46	Executing build CT - User Security - Default Job #14 (CT-US-JOB1-14)
simple	14-Mar-2016 17:41:46	Skipping execution of task 'Checkout Default Repository' as it is disabled
simple	14-Mar-2016 17:41:46	Running pre-build action: VCS Version Collector
simple	14-Mar-2016 17:41:47	Skipping execution of task 'Checkout Default Repository' as it is disabled
simple	14-Mar-2016 17:41:47	Skipping execution of task 'NuGet Package Restore' as it is disabled
simple	14-Mar-2016 17:41:47	Skipping execution of task 'Build User Security' as it is disabled
simple	14-Mar-2016 17:41:47	Starting task 'Set Version Variable' of type 'com.atlassian.bamboo.plugins.scripttask:task.builder.script'
command	14-Mar-2016 17:41:47	Beginning to execute external process for build 'CT - User Security - Default Job #14 (CT-US-JOB1-14)'\n ... running command line: \nC:\Users\dmiller\AppData\Local\Temp\CT-US-JOB1-14-ScriptBuildTask-753372486520642903.ps1\n ... in: C:\Users\dmiller\bamboo-agent-home\xml-data\build-dir\CT-US-JOB1\n ... using extra environment variables: \nbamboo_planRepository_1_branch=master\nbamboo_repository_revision_number=02aeb1cb3549a4e900fc225816263975d3d37131\nbamboo_repository_360449_previous_revision_number=02aeb1cb3549a4e900fc225816263975d3d37131\nbamboo_capability_system_builder_msbuild_MSBuild_v14_0__64bit_=C:\Program Files (x86)\MSBuild\14.0\bin\amd64\MSBuild.exe\nbamboo_resultsUrl=https://criticaltech.atlassian.net/builds/browse/CT-US-JOB1-14\nbamboo_capability_system_jdk_JDK_1_8_0_73=C:\Program Files\Java\jre1.8.0_73\nbamboo_capability_system_builder_msbuild_MSBuild_v4_0__32bit_=C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe\nbamboo_repository_360449_name=CT\nbamboo_planRepository_1_name=CT\nbamboo_build_working_directory=C:\Users\dmiller\bamboo-agent-home\xml-data\build-dir\CT-US-JOB1\nbamboo_buildKey=CT-US-JOB1\nbamboo_shortPlanName=User Security\nbamboo_repository_360449_revision_number=02aeb1cb3549a4e900fc225816263975d3d37131\nbamboo_planRepository_name=CT\nbamboo_buildNumber=14\nbamboo_shortJobName=Default Job\nbamboo_buildResultsUrl=https://criticaltech.atlassian.net/builds/browse/CT-US-JOB1-14\nbamboo_planRepository_repositoryUrl=https://bitbucket.org/airvault/ct\nbamboo_agentId=327681\nbamboo_planName=CT - User Security\nbamboo_shortPlanKey=US\nbamboo_repository_360449_branch_name=master\nbamboo_shortJobKey=JOB1\nbamboo_planRepository_revision=02aeb1cb3549a4e900fc225816263975d3d37131\nbamboo_repository_previous_revision_number=02aeb1cb3549a4e900fc225816263975d3d37131\nbamboo_buildTimeStamp=2016-03-14T17:41:42.328-05:00\nbamboo_planRepository_previousRevision=02aeb1cb3549a4e900fc225816263975d3d37131\nbamboo_repository_360449_git_branch=master\nbamboo_buildResultKey=CT-US-JOB1-14\nbamboo_repository_git_branch=master\nbamboo_repository_branch_name=master\nbamboo_buildPlanName=CT - User Security - Default Job\nbamboo_repository_360449_git_username=racingcow\nbamboo_planRepository_1_revision=02aeb1cb3549a4e900fc225816263975d3d37131\nbamboo_repository_name=CT\nbamboo_version=0.0.0.0\nbamboo_dependenciesDisabled=false\nbamboo_planRepository_branch=master\nbamboo_repository_360449_git_repositoryUrl=https://bitbucket.org/airvault/ct\nbamboo_agentWorkingDirectory=C:\Users\dmiller\bamboo-agent-home\xml-data\build-dir\nbamboo_planRepository_1_previousRevision=02aeb1cb3549a4e900fc225816263975d3d37131\nbamboo_repository_git_username=racingcow\nbamboo_planRepository_1_type=bb\nbamboo_planRepository_branchName=master\nbamboo_capability_system_jdk_JDK=C:\Program Files\Java\jre1.8.0_73\nbamboo_planRepository_type=bb\nbamboo_planRepository_1_username=racingcow\nbamboo_ManualBuildTriggerReason_userName=racingcow\nbamboo_capability_system_builder_msbuild_MSBuild_v14_0__32bit_=C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe\nbamboo_repository_git_repositoryUrl=https://bitbucket.org/airvault/ct\nbamboo_capability_system_builder_msbuild_MSBuild_v4_0__64bit_=C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe\nbamboo_working_directory=C:\Users\dmiller\bamboo-agent-home\xml-data\build-dir\CT-US-JOB1\nbamboo_planKey=CT-US\nbamboo_planRepository_1_repositoryUrl=https://bitbucket.org/airvault/ct\nbamboo_planRepository_username=racingcow\nbamboo_capability_system_jdk_JDK_1_8=C:\Program Files\Java\jre1.8.0_73\nbamboo_planRepository_1_branchName=master\n
simple	14-Mar-2016 17:41:47	Failing task since return code of [powershell -ExecutionPolicy bypass -Command C:\Users\dmiller\AppData\Local\Temp\CT-US-JOB1-14-ScriptBuildTask-753372486520642903.ps1] was -1 while expected 0
simple	14-Mar-2016 17:41:47	Finished task 'Set Version Variable' with result: Failed
simple	14-Mar-2016 17:41:47	Running post build plugin 'Docker Container Cleanup'
simple	14-Mar-2016 17:41:47	Running post build plugin 'NCover Results Collector'
simple	14-Mar-2016 17:41:47	Running post build plugin 'Clover Results Collector'
simple	14-Mar-2016 17:41:47	Running post build plugin 'npm Cache Cleanup'
simple	14-Mar-2016 17:41:47	Running post build plugin 'Artifact Copier'
simple	14-Mar-2016 17:41:47	Finalising the build...
simple	14-Mar-2016 17:41:47	Stopping timer.
simple	14-Mar-2016 17:41:47	Build CT-US-JOB1-14 completed.
simple	14-Mar-2016 17:41:44	Running on server: post build plugin 'NCover Results Collector'
simple	14-Mar-2016 17:41:44	Running on server: post build plugin 'Build Hanging Detection Configuration'
simple	14-Mar-2016 17:41:44	Running on server: post build plugin 'Clover Delta Calculator'
simple	14-Mar-2016 17:41:44	Running on server: post build plugin 'Maven Dependencies Postprocessor'
simple	14-Mar-2016 17:41:44	All post build plugins have finished
simple	14-Mar-2016 17:41:44	Generating build results summary...
simple	14-Mar-2016 17:41:44	Saving build results to disk...
simple	14-Mar-2016 17:41:44	Logging substituted variables...
simple	14-Mar-2016 17:41:44	Indexing build results...
simple	14-Mar-2016 17:41:44	Finished building CT-US-JOB1-14.

I've tried adding "Exit 0" to the end of the script, etc. to no avail.

Does anyone know what is causing this?

8 answers

1 accepted

9 votes
Answer accepted
David Miller March 23, 2016

I don't have node installed on the remote agent, but this stackoverflow post indicates that the build agent may not be merging user and system PATH environment variables in Windows. Just to see if that was the case, I 1) opened the Environment Variables applet, 2) merged the user-level PATH environment information in to the system-level PATH and 3) removed the user-level PATH variable as the answer on that post suggests (see screenshot).

After restarting the remote build agent and re-running the build, the PowerShell script is now running successfully!

 

2016-03-23_13-31-00.png

justinhp
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 5, 2017

Thank you!  For me I just had an empty Path value for the User.  Once I removed the Path variable, everything started working!!!

Mitch Thompson November 5, 2018

This is great, but incredibly difficult to solve for automated deployments.  Since the user profile is created upon first login, when exactly are you supposed to delete the path since no one ever logs in?

Mitch Thompson November 6, 2018

That worked out.  Check out this stuff:

Remove-ItemProperty -Path 'Registry::HKEY_USERS\.DEFAULT\Environment\' -Name PATH

 

Poof. Now the service, running under the LocalSystem profile, uses the System Path variable.

Like # people like this
0 votes
Bill Rebey
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 29, 2020

I had this problem intermittently.  

For me, the problem was that my Deployment Agent restrictions (Specifically "Windows exists") somehow got lost, so sometimes one of our Linux agents would pick up the job, and of course Powershell or Batch files can't be run by Linux.    Be sure you're running on a Windows build agent.

Robert Praetorius January 29, 2020

Which reminds me. . .running Linux no longer implies that you can't run PowerShell.  And most Windows systems with a git client (which is probably most Windows systems used by developers) have a reasonable bash implementation.  I should probably head over to JIRA for Bamboo and check and see if support for these has been suggested (or implemented) yet.

[later edit - I have added this request as a comment to https://jira.atlassian.com/browse/BAM-15825 .  BAM-15825 actually requests the opposite (that PowerShell be disallowed on non-Windows systems), but has been rendered obsolete by foot-dragging^H^H^H^H^H^H^H^H^H^H^H^H^Hthe passage of time.]

Like Jeyanthan I likes this
0 votes
Bobby Tait December 10, 2019

In the "Duh" solution department, I came across the same error when I accidentally left Interpreter as "Shell", instead of changing it to "Windows PowerShell".

William
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 23, 2020

LOL. Been there done that.

0 votes
Mitch Thompson November 6, 2018

Try letting the agent run as localservice, then execute this powershell command.

Remove-ItemProperty -Path 'Registry::HKEY_USERS\.DEFAULT\Environment\' -Name PATH

 

 

onassis.cayetano
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 29, 2020

Thank you for this tip.  Removing the registry entry for PATH for the .DEFAULT user fixed the issue for me.

For additional context, this is what happened in our setup:
1. We initially installed the agent as a standard dedicated user.

2. Then we switched it to run as a Windows service and then as the System user.  After this, we couldn't execute any PowerShell commands.

Pramod Thirunahari
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 7, 2021

Hi Mitch, I have removed the path from registry but still issue seems not resolved. Is there any additional steps have to be done after removing the path from registry?

0 votes
David Miller March 23, 2016

Thanks for the response, @Kalle Sirkesalo. That command that referenced is actually something that Bamboo controls. The inline script that I type into the Bamboo interface is placed into that T-US-JOB1-14-ScriptBuildTask-753372486520642903.ps1 file, and then Bamboo generates the command to run it while the build is being executed. I don't think I can make Bamboo Cloud or the Remote Agent use the -File switch instead of -Command, but let me know if you know of some config setting for that.

To make sure my user had permissions, I logged in to the remote agent machine as dmiller and created a test.ps1 file in that same folder with my single line of script. Then I ran the same command, and it seemed to run fine with both the -File and -Command switches (see screenshot).

2016-03-23_11-34-28.png

Atul
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 21, 2019

Hello David, 

I am also facing the same issue , Bamboo remote agent is not able to run the powershell command whereas when using directly from the powershell it is working fine. Here is the error I get when I run using the Bamboo

 

Failing task since return code of [powershell -NonInteractive -ExecutionPolicy bypass -Command C:\Users\Missionuser\bamboo-agent-home\temp\SDM-SDCE2-JOB1-240-ScriptBuildTask-5153737850206120377.ps1] was -1 while expected 0
Robert Praetorius August 22, 2019

If you've followed the links and tried the suggestions in this thread and you're still not getting any results, I'd recommend monitoring the task using procmon.

(I'm not familiar with Bamboo Cloud.  I use a local Bamboo installation.  There may or may not be difficulties running procmon in the cloud.  Perhaps other people on the thread could speak to that).

If you end up using procmon, you'll want to start procmon just before the test runs and stop it just after the task finishes - procmon captures a lot of events and you'll spend less time wading through irrelevant stuff if you narrow down the time window.

PowerShell may sometimes fail in a way the procmon doesn't reveal, but that's pretty rare.

0 votes
Kalle Sirkesalo March 21, 2016

14-Mar-2016 17:41:47    Failing task since return code of [powershell -ExecutionPolicy bypass -Command C:\Users\dmiller\AppData\Local\Temp\CT-US-JOB1-14-ScriptBuildTask-753372486520642903.ps1] was -1 while expected 0

The keyword here would be is the machine allowing you to run powershell scripts as this user that is executing. Meaning have you checked the execution policy command that it works in your case.

I would use -File instead of -Command here for example.

Both popular ways ran from cmd:


PowerShell.exe -ExecutionPolicy Bypass -File .runme.ps1


PowerShell.exe -ExecutionPolicy UnRestricted -File .runme.ps1

 

0 votes
David Miller March 21, 2016

I have. The same error occurs when adding "Return 0" to the end of the script, unfortunately.

0 votes
Kalle Sirkesalo March 21, 2016

Have you tried to add Return 0

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events