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

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.comsimple	14-Mar-2016 17:41:46	Remote agent on host DEV-BUILD01.criticaltech.comsimple	14-Mar-2016 17:41:46	Build working directory is C:\Users\dmiller\bamboo-agent-home\xml-data\build-dir\CT-US-JOB1simple	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 disabledsimple	14-Mar-2016 17:41:46	Running pre-build action: VCS Version Collectorsimple	14-Mar-2016 17:41:47	Skipping execution of task 'Checkout Default Repository' as it is disabledsimple	14-Mar-2016 17:41:47	Skipping execution of task 'NuGet Package Restore' as it is disabledsimple	14-Mar-2016 17:41:47	Skipping execution of task 'Build User Security' as it is disabledsimple	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\nsimple	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 0simple	14-Mar-2016 17:41:47	Finished task 'Set Version Variable' with result: Failedsimple	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 finishedsimple	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?  
[](https://community.atlassian.com/t5/Bamboo-questions/Bamboo-Powershell-script-always-fails-with-quot-Failing-task/qaq-p/263898?attachment-id=12411){#link_1167720b551e0b}  
[](/tnckb94959/attachments/tnckb94959/bamboo-questions/8370/1/2016-03-23_11-34-28.png){#display_1167720b551e0b_0}


Answers:
author: David Miller
body: I don't have node installed on the remote agent, but [this stackoverflow post](http://stackoverflow.com/q/30183168/95763) 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  
[](https://community.atlassian.com/t5/Bamboo-questions/Bamboo-Powershell-script-always-fails-with-quot-Failing-task/qaq-p/263898?attachment-id=6304){#link_11677210a595b1}  
[](/tnckb94959/attachments/tnckb94959/bamboo-questions/8375/1/2016-03-23_13-31-00.png){#display_11677210a595b1_0}

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


author: Mitch Thompson
body: 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?


author: Robert Praetorius
body: This may (or may not, depending on context) help: <https://serverfault.com/questions/397966/how-to-set-environment-variable-for-another-user>

(see also <https://jira.atlassian.com/browse/BAM-17389>)


author: Mitch Thompson
body: 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.


author: Bill Rebey
body: 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.

comments: author: Robert Praetorius
body: 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.\]


author: Bobby Tait
body: 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".

comments: author: William
body: LOL. Been there done that.


author: Mitch Thompson
body: Try letting the agent run as localservice, then execute this powershell command.

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

comments: author: onassis.cayetano
body: 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.


author: Pramod Thirunahari
body: 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?


author: David Miller
body: 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

comments: author: Atul
body: 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
```


author: Robert Praetorius
body: 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](https://docs.microsoft.com/en-us/sysinternals/downloads/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](https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/33338296-bug-binary-module-loading-fails-silently-on-fall), but that's pretty rare.


author: Kalle Sirkesalo
body: **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:

<br />

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

<br />

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


author: David Miller
body: I have. The same error occurs when adding "Return 0" to the end of the script, unfortunately.


author: Kalle Sirkesalo
body: Have you tried to add Return 0


Tags: powershell

stop