It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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?

6 answers

1 accepted

7 votes
Answer accepted

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

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

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?

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

Have you tried to add Return 0

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

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

 

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 Aug 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

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.

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

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

 

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bamboo

Unable to add or edit Bitbucket Cloud repository in Bamboo

On 31 May, a GDPR-related change went live in the Bitbucket Cloud API that resulted in users not being able to create or edit Bitbucket Cloud Linked repositories in Bamboo. This API update removed t...

665 views 2 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you