is it possible passing winrm(powershell) scripts output from a job to another?

264 views
Skip to first unread message

mutlu özel

unread,
Jul 30, 2019, 7:39:38 AM7/30/19
to rundeck-discuss
Hello All,

I have two powershell script and i want to transfer the output from first script to second script.

for example;

first script include only below command;

hostname


the second script needs to return value from first script output for ping example;

ping $returnValue


Could you please help me, how can i collect the output from ps1 script and how do i transfer the data to another job?

note: i already read the global variable and log filter options but i didn't understood. 
if it is easy, could you please explain me on above example

many thanks for your help

best regards

mutlu


Reiner Acuña

unread,
Jul 30, 2019, 9:50:06 AM7/30/19
to rundeck-discuss
Hey Mutlu,

Let's do it:

You can pass data between steps, jobs, and scripts using data passing feature, please take a look:


I did a basic example with two PowerShell scripts, the first generates value and the second uses an argument to get the first value. Rundeck get the value of the first script (using a regex to store on a ${data.xxx} variable and pass as argument to the second script.

script1.ps1:

$strString = "file=myfile"
write
-host $strString

script2.ps1:

param (
 
[string]$server = "default-data"
 
)
 write
-output $server

And here the job definition:

<joblist>
 
<job>
   
<defaultTab>summary</defaultTab>
   
<description></description>
   
<executionEnabled>true</executionEnabled>
   
<id>fbeacd6f-929f-41f8-a151-89e449457a49</id>
   
<loglevel>INFO</loglevel>
   
<name>JobExample</name>
   
<nodeFilterEditable>false</nodeFilterEditable>
   
<scheduleEnabled>true</scheduleEnabled>
   
<sequence keepgoing='false' strategy='node-first'>
     
<command>
       
<exec>echo "starting..."</exec>
     
</command>
     
<command>
       
<fileExtension>ps1</fileExtension>
       
<plugins>
         
<LogFilter type='key-value-data'>
           
<config>
             
<logData>true</logData>
             
<name>str</name>
             
<regex>.*file=(.*).*</regex>
           
</config>
         
</LogFilter>
       
</plugins>
       
<scriptargs />
       
<scriptfile>C:\rundeck\projects\ProjectPOWERSHELL/script1.ps1</scriptfile>
       
<scriptinterpreter>powershell.exe</scriptinterpreter>
     
</command>
     
<command>
       
<fileExtension>ps1</fileExtension>
       
<scriptargs>${data.str}</scriptargs>
       
<scriptfile>C:\rundeck\projects\ProjectPOWERSHELL\script2.ps1</scriptfile>
       
<scriptinterpreter>powershell.exe</scriptinterpreter>
     
</command>
   
</sequence>
   
<uuid>fbeacd6f-929f-41f8-a151-89e449457a49</uuid>
 
</job>
</joblist>

A good place to understand more this way of passing data is this thread, take a look if you want:


Hope it helps!

mutlu özel

unread,
Aug 2, 2019, 1:47:19 AM8/2/19
to rundeck-discuss
Hello Reiner,

I successfully applied your example and it worked. Many thanks for your help

I have last two question, could you please help me;

- Question-1: I understood, we can transfer the string data between steps with key-value-data ${data.str} in the same job, but i want to transfer the data to another job. Can we transfer the same data to another job? 

- Question-2: how can i transfer the array data, between steps and/or between jobs? ( for example; i used sql select command and returned hundreds records on first step. And i want to transfer all records to second step or to different job )

Best Regards

Mutlu

Reiner Acuña

unread,
Aug 2, 2019, 5:59:09 PM8/2/19
to rundeck-discuss
Hello Mutlu,

Question 1:

Yes, you can do it using Job Reference Step: https://docs.rundeck.com/docs/manual/node-steps/builtin-node-steps.html#job-reference-step

I did a little basic example to see that:

Job1 (Parent job):

<joblist>
<job>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>80156d9b-5279-49dd-bb45-1e349224b170</id>
<loglevel>INFO</loglevel>
<name>JobONE</name>
<nodeFilterEditable>false</nodeFilterEditable>
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<plugins>
<LogFilter type='key-value-data'>
<config>
<logData>true</logData>
<regex>^(MYKEY1)\s*=\s*(.+)$</regex>
</config>
</LogFilter>
</plugins>
<script><![CDATA[echo "MYKEY1=value-one"
echo "MYKEY2=value-two"
echo "MYKEY3=value-three"
]]></script>
<scriptargs />
</command>
<command>
<jobref name='JobTWO' nodeStep='true'>
<arg line='-myoption ${data.MYKEY1}' />
<uuid>a204db10-4ee2-4041-b3e5-ef6087de6908</uuid>
</jobref>
</command>
</sequence>
<uuid>80156d9b-5279-49dd-bb45-1e349224b170</uuid>
</job>
</joblist>

Job2 (Child job):

<joblist>
<job>
<context>
<options preserveOrder='true'>
<option name='myoption' />
</options>
</context>
<defaultTab>nodes</defaultTab>
<description></description>
<executionEnabled>true</executionEnabled>
<id>a204db10-4ee2-4041-b3e5-ef6087de6908</id>
<loglevel>INFO</loglevel>
<name>JobTWO</name>
<nodeFilterEditable>false</nodeFilterEditable>
<scheduleEnabled>true</scheduleEnabled>
<sequence keepgoing='false' strategy='node-first'>
<command>
<exec>echo "the value of first job is: ${option.myoption}"</exec>
</command>
</sequence>
<uuid>a204db10-4ee2-4041-b3e5-ef6087de6908</uuid>
</job>
</joblist>

Question 2:


Regards!

mutlu özel

unread,
Aug 5, 2019, 4:30:18 AM8/5/19
to rundeck-discuss
It  worked, many thanks and thank you for your support.

Best Regards

Mutlu
Reply all
Reply to author
Forward
0 new messages