3

I am looking to grab the result of a batch file that is executed within a Jenkins pipeline Groovy script.

I know that I can do this:

def result = "pem.cmd Test_Application.pem".execute().text

However, I need to run a batch of commands and grab the result of the batch file. That example above only has one command. I need to first change directory and then execute the "cmd" file with a parameter. So I attempted the following:

def cmd = new StringBuilder()
cmd.append("CD \"${path}\"\n")
cmd.append("IF %ERRORLEVEL% NEQ 0 EXIT /B %ERRORLEVEL%\n")
cmd.append("pem.cmd Test_Application.pem\n")
//echo bat(returnStdout: true, script: cmd.toString())
def result = bat cmd.toString()
echo result

The "result" variable is null even though the log shows that the command did return a result. I know I could output the batch file results to text file, and read the text file, but I would just like to see if I can grab the result, like I attempted above. Any help is appreciated.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Grinder
  • 151
  • 1
  • 2
  • 8
  • Have you tried to use something like [this](https://stackoverflow.com/a/38783622/10721592)? Try to put execution of your batch file into `script` section. – biruk1230 Jan 31 '19 at 15:14
  • Thank you for your response. I was able to get it to work using bat command. I tried this initially, but I didn't do it right. – Grinder Jan 31 '19 at 16:59

1 Answers1

4

Ok, I got it work as follows:

def cmd = new StringBuilder()
cmd.append("CD \"${path}\"\n")
cmd.append("pem.cmd Test_Application.pem\n")

def x = bat(
    returnStdout: true,
    script: "${cmd.toString()}"
)

echo x

That does it.

Grinder
  • 151
  • 1
  • 2
  • 8