How to run powershell scripts from bamboo task?

I'm having a heck of a time with this... burned a lot of time.

I have a script.ps1 file, in this file is a powershell function 'my_script' that takes an argumment $foo1 so like this:

script.ps1:

 

#requires -Version 2.0
function my_script {
	param
	(
		[String]$foo1 = $(throw "Paramerter -foo1 [System.String] is required.")
	)
 
    begin
    {
        #---------- setup string variables --------------#
        $MyToolLocation = "{0}\someplace\tools\" -f $foo1
        ...
    }
    process
    {
        ...
    }
    end
	{
        ...
    }
}

this script is located in my workspace  from the root under Scripts\powerShell

I set my build task trying to use this as:

Script Location

[File]

[x] Run as Powershell script

Script file 

script.ps1       ** I've also tried this: ${bamboo.build.working.directory}\Scripts\powershell\script.ps1

Argument

 

my_script -foo1 'Does not matter'

Working sub directory

Scripts\powershell   ** I've also tried this: ${bamboo.build.working.directory}\Scripts\powershell


and no matter what I try I get this error:

Unable to find executable at C:\BuildDir\3112961-5472257\Scripts\powershell\script.ps1. Will try to run it anyway.

Executable? this is a script... but whatever... this script is in this location.

My only recourse to this point is to put my script inline, there must be some option for running from a script.. .how I do this from command line is:

powershell -ExecutionPolicy unrestricted command "& {.' command "& { . 'c:\work\Scripts\powershell\script.ps1'; my-function -foo1 'Does Not Matter'"

Certainly this must be repeatable in Bamboo? How?

7 answers

1 votes

Hello Karl,

Thank you for your update.

Could you please attempt to run the following script.ps1:

Param($intOne = 5, $intTwo = 3)
Function add-numbers([int]$intOne, [int]$intTwo)
{
	$intOne + $intTwo
}
Function Test([string]$var1, [string]$var2)
{
	Write-Host: "`$var1 value: $var1"
	Write-Host: "`$var2 value: $var2" 
}
  1. place the script above under a Build Working Directory our create a Source Code Checkout task
  2. create a Script task
  3. select Run as Powershell script
  4. in the Script body*, please add the following:

    . .\script.ps1
    add-numbers -intOne 1 -intTwo 2
    Test "ABC" "DEF"

    The first 'dot' asks PowerShell to load the script file into your PowerShell environment, not to start it. You should also use set-ExecutionPolicy Unrestricted or set-ExecutionPolicy AllSigned

You should expect the following output from the above:

3
$var1 value: ABC
$var2 value: DEF

If you find this answer useful, I would kindly ask you to accept it so the same will be visible to others who might be facing the same issue you have inquired.

Thank you for your understanding.

Kind regards,
Rafael P. Sperafico
Atlassian Support

I run a powershell script in one of my deployment projects that I manage with Bamboo.  I believe I went through the same problem as you.  Here's how I got it to work - I used a Command-type task instead of a Script-type task to execute the script.  When configuring your Command, select Windows PowerShell for the Executable field.  In the Argument field of the Command configuration, I specified ${bamboo.build.working.directory}\<scriptFileName>.ps1 args...  Try that instead of using the Script-type task.

0 votes

Hello Karl,

Thank you for your question.

Could you please create the following PowerShell script by calling it Argument.PS1:

## Top of the script
param (
   [string]$MyArgument
)
$MyArgument

In Bamboo, please:

  1. place the script above under a Build Working Directory our create a Source Code Checkout task
  2. create a Script task
  3. select Run as Powershell script
  4. now, you have two options (run it inline or as a file)
  5. if you choose inline, please add the following to the Script task:

    ./Argument.PS1 Hello
  6. if you choose file, please add the following in Script file*:

    ./Argument.PS1
  7. in Argument, please add the following:

    Hello

You should be able to run the PowerShell script above without any grief.

In case you have placed your PowerShell script under a directory (i.e.: Directory), please, run it as:

./Directory/Argument.PS1 Hello

If you find this answer useful, I would kindly ask you to accept it so the same will be visible to others who might be facing the same issue you have inquired.

Thank you for your understanding.

Kind regards,
Rafael P. Sperafico
Atlassian Support

Rafael, this is exactly what I'm doing per my hopefully (overly?) descriptive original posting. So no, it's not working.

NOTE: 

Well, not exactly, I need to call a function in my script file, not just load the script. Your answer implies I can't do this.

Also, what is the process for running dos cmd/batch executables out of a bamboo task? I'm a bit worried about this given what I'm NOT seeing via google searches. 

I seem to be having the same issue with powershell.

My set up is Bamboo is on a Linux server and I am trying to run a powershell script on a remote Windows server. All my Linux box deployment tasks work ok as they are using ssh to communicate.

I just cannot get the remote windows server to run the script. I am wondering this only works if the bamboo is inatalled on a windows box which has powershell.

Please read my question at this url: http://stackoverflow.com/questions/38892583/bamboo-deploy-using-powershell-script

I think I might need a remote Bamboo agent running on the Windows server to get this to work.

The command task approach mentioned above does not work because I have a Linux server hosting Bamboo and so there is no Powershell executable.

Rafael you seem very knowledgable in this area - please could you give me any advice?

 

Many thanks.

I know this is really old but I ran into the same issue recently with Bamboo 5.8.1. As it turns out, having spaces and/or parentheses in the path to the PS script was causing the error, though annoyingly it would still run. The result of this was that a failed build would show the "Unable to find executable at C:\path\to\build-dir\script.ps1. Will try to run it anyway." Developers saw this and decided THAT was the issue and not their branch failing to build and blamed Bamboo.

Solution: Change the path to be spaceless and contain no funky characters (I switched to underscores) and the error went away.

 

Suggest an answer

Log in or Join to answer
Community showcase
Renan Battaglin
Published May 18, 2017 in Bamboo

FAQ: How to Upgrade Bamboo Server

Bamboo 5.9 will no longer be supported after June 12, 2017. What does this mean? As part of our End of Life policy, Atlassian supports major versions for two years after the first major iteratio...

1,073 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot