Executing Powershell script that requires arguments from Jenkins pipeline

Viewed 1722

I've got a project that looks like this:

project
|
├───src
│       Utility.groovy
│
└───vars
        pipeline.groovy
        script.ps1

In pipeline.groovy I have utility object:

def util = new Utility(this)

and a step that calls util.scriptCall(arg1: "1", arg2: "2", arg3: "3"), which is defined as:

this.context.powershell returnStdout: true, script: ".\\script.ps1 -Arg1 ${args.arg1} -Arg2 ${args.arg2} -Arg3 ${args.arg3} -Workspace ${this.context.env.workspace}"

From everything I've found I'm supposedly invoking this correctly, however I'm getting the following error:

.\script.ps1 : The term '.\script.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I've got the Powershell plugin installed but the lack of documentation for anything related to Jenkins isn't helping. I've moved script.ps1 into the src folder and even tried making a scripts folder for this with the same results.

Any idea why my invoking a script is getting interpreted as a cmdlet?

2 Answers

Because the script is not available in the location, where you're trying to invoke the script.

Use the absolute path to invoke the script. If the path includes whitespaces use the call operator (&).

& "absolutePathToScript/script.ps1" 

I'm not hundred percent sure, but Jenkins will inject the workspace via $env:workspace (at least it will do so in normal PowerShell build steps), so you be able to invoke the script via:

& "$($env:workspace)/vars/script.ps1"

I'm just a ding dong. The Workspace was the Git repo that was getting built. I had to relocate the script to that project and it worked as expected.

Related