Search code examples
powershellrundeck

Rundeck Powershell pass options to script


My question is how do I pass options/ parameter into a PowerShell script? Could not find it on the docs or any other good post about it. I know it's possible because I saw it in a video from the rundeck YouTube channel.


Solution

  • Using inline script just add @option.youroption@, make sure to fill the right params to execute in your windows box. I leave a couple of examples, take a look:

    Job definition (inline PowerShell script):

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='opt1' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <dispatch>
          <excludePrecedence>true</excludePrecedence>
          <keepgoing>false</keepgoing>
          <rankOrder>ascending</rankOrder>
          <successOnEmptyNodeFilter>false</successOnEmptyNodeFilter>
          <threadcount>1</threadcount>
        </dispatch>
        <executionEnabled>true</executionEnabled>
        <id>700b58f6-9bae-4d97-b97d-dd4c7efba9ca</id>
        <loglevel>INFO</loglevel>
        <name>JobWINRM</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <nodefilters>
          <filter>name: windows</filter>
        </nodefilters>
        <nodesSelectedByDefault>true</nodesSelectedByDefault>
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <exec>echo "hi"</exec>
          </command>
          <command>
            <fileExtension>.ps1</fileExtension>
            <script><![CDATA[Write-Host "@option.opt1@"]]></script>
            <scriptargs />
            <scriptinterpreter>powershell.exe</scriptinterpreter>
          </command>
        </sequence>
        <uuid>700b58f6-9bae-4d97-b97d-dd4c7efba9ca</uuid>
      </job>
    </joblist>
    

    Using "external script" passing some parameter in "Arguments" textbox.

    Powershell script:

    ### hello.ps1 ###
    Param($Variable1 = "Hello", $Variable2 = "World")
    "$Variable1 $Variable2"
    

    Rundeck Job definition:

    <joblist>
      <job>
        <context>
          <options preserveOrder='true'>
            <option name='opt1' />
          </options>
        </context>
        <defaultTab>nodes</defaultTab>
        <description></description>
        <dispatch>
          <excludePrecedence>true</excludePrecedence>
          <keepgoing>false</keepgoing>
          <rankOrder>ascending</rankOrder>
          <successOnEmptyNodeFilter>false</successOnEmptyNodeFilter>
          <threadcount>1</threadcount>
        </dispatch>
        <executionEnabled>true</executionEnabled>
        <id>854a8639-ad98-4520-b446-359ec2eaf531</id>
        <loglevel>INFO</loglevel>
        <name>JobWINRMEXT</name>
        <nodeFilterEditable>false</nodeFilterEditable>
        <nodefilters>
          <filter>name: windows</filter>
        </nodefilters>
        <nodesSelectedByDefault>true</nodesSelectedByDefault>
        <scheduleEnabled>true</scheduleEnabled>
        <sequence keepgoing='false' strategy='node-first'>
          <command>
            <exec>echo "hi"</exec>
          </command>
          <command>
            <fileExtension>.ps1</fileExtension>
            <scriptargs>"${option.opt1}"</scriptargs>
            <scriptfile>/home/user/scripts/hello.ps1</scriptfile>
            <scriptinterpreter>powershell.exe</scriptinterpreter>
          </command>
        </sequence>
        <uuid>854a8639-ad98-4520-b446-359ec2eaf531</uuid>
      </job>
    </joblist>