2

I have a Groovy script that runs a separate Powershell script as part of a Jenkins pipeline. The Powershell script returns a value (int) and I intend to use that value in the Groovy script by assigning it to a variable but the groovy variable is always evaluated to null. Code below, edited for brevity.

Powershell Script

#Some actions...
$foo = 0
return $foo

Returns (when run manually in Powershell):

0

Groovy Script

stage('StageX'){
    //Some actions...
    def return_val = powershell "Path\\to\\script\\someScript.ps1"
    echo return_val
    if (return_val == 0){
        //Do things...
    }
}

Returns:

null

I know that the script actually runs as it performs other actions that are apparent when the Groovy script runs, I just can't seem to get Groovy to pick up the return value of the Powershell script.

What is the best way to do this?

2 Answers 2

7

You need to pass it the returnStatus parameter:

    def return_val = powershell script: "Path\\to\\script\\someScript.ps1", returnStatus: true

See the documentation for more information https://www.jenkins.io/blog/2017/07/26/powershell-pipeline/

Sign up to request clarification or add additional context in comments.

Comments

0

Im kinda to new to powershell but what can be expected from a 0. Zero value. Nothing.

if (return_val == Null){
        //Do things...
    } 

Have you tested this? Do you get another values back beside 0?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.