logo

NJP

Passing an object from PowerShell custom activity to workflow

Import · Mar 24, 2017 · article

I wanted to pass an object from PowerShell to my workflow, instead of creating individual standard out tags that had to be individually parsed. This is a working process I found. You will need to:

  1. Create a PowerShell script
  2. Create a custom activity using the Activity Designer
  3. Add the custom activity to a workflow

Create the PowerShell script

PowerShell w/ JSON

PowerShell handles JSON data very well, either parsing JSON passed in or creating JSON to pass out I first created an object in PowerShell and added properties with the data I wanted in my workflow. This could be accomplished with any object but for demo purposes, I created my object manually:

$myOutput = @{}

$myOutput.name = "Chad" #Hi

$myOutput.age = 21 #oh to be young again

$myOutput.eye_color = "Brown" #sort of

$myOutput.car = "1965 Ford Mustang" #one of my all time favorites

To output the object, first it must be converted to JSON

$myData = $myOutput | ConvertTo-Json

Then write out the data. Notice that I am not using the standard out tags "%%output%%"

Write-Host $myData

PowerShell w/ XML

XML takes a little more effort to generate, but it is easy to read and may be more familiar to some. Using the same object as before, create the XML string with data included. This I found in the wiki

$data = @"

     

              $($myOutput.name)

              $($myOutput.age)

              $($myOutput.eye_color)

              $($myOutput.car)

     

"@

Then write out the data. Again, not using tags:

Write-Host $data

In the case of XML, you wouldn't necessarily need to create the $myOutput object first. You could simply create the $data string and include any data or variables that you needed.

The whole code should look something like this:

$myOutput = @{}

$myOutput.name = "Chad" #Hi

$myOutput.age = 21 #oh to be young again

$myOutput.eye_color = "Brown" #sort of

$myOutput.car = "1965 Ford Mustang" #one of my all time favorites

$myData = $myOutput | ConvertTo-Json

Write-Host $myData

-OR-

$myOutput = @{}

$myOutput.name = "Chad" #Hi

$myOutput.age = 21 #oh to be young again

$myOutput.eye_color = "Brown" #sort of

$myOutput.car = "1965 Ford Mustang" #one of my all time favorites

$data = @"

     

              $($myOutput.name)

              $($myOutput.age)

              $($myOutput.eye_color)

              $($myOutput.car)

     

"@

Write-Host $data

Create a new MID Server script file: MID Server > Script Files

Copy/paste your PowerShell code into the script field

Create the custom activity

Open the Workflow editor: Orchestration > Workflow Editor

Select the Custom tab and click the plus

Select Powershell

Enter a name for the activity and click Continue

For demonstration purposes, my custom activity does not have any input variables.

Click Continue

Under Execution Command, enter the following:

  • - Target host: 127.0.0.1
  • - Script type: MID Server script file
  • - MID Server script file: Select the PowerShell script file

Click Continue

Now, here comes the juicy part

Create output variable and map the values

At this point click Test Inputs. The activity will run your script and return results. Click output. You should see your resulting data object.

image - image

Click Save for parsing rules. This will give you data to work with when creating parsing rules.

Click the plus to the right of {} Output

Enter a name for the output variable and change the type dropdown to Object

Press Enter and you will see another plus to the right of your new variable

Click that plus to create as many object attributes as necessary

imageimage

For the demo, I created four; one for each attribute that I was passing from PowerShell

Hover your mouse over the name of the first attribute; your pointer will turn into a fist

Click and drag to the grey box under Parsing rules > Variable name

You will see the box populated with activityOutput..

A popup will appear titled Parsing rule for .

imageimage

Set the Parsing source to executionResult.output

Select the appropriate Parsing Type. For this demo either JSON or XML

To set the expression, hover over the Sample payload data and click the attribute you want to assign to this variable. You should see the data you wanted in the Parsing result field and the Expression field should be filled in with an appropriate value.

Click Submit

Once you have created parsing rules for all of your variables, click Continue.

image

The final step for your activity is to create conditions. This will create the paths that your activity will be able to take in the workflow. For this demo I simply created an Always. For a production activity you would likely want to create a Success and a Failure path.

image

Publish the activity and add it to a test workflow. I created a simple workflow on the Global table with the custom activity and a Run Script to show parsing the data.

image

When you run this workflow, assuming it is successful, hover over the activity you created to see the Databus output:

image

In the Run Script, I used workflow.info to write the data out to the workflow log:

workflow.info("****name: " + data.get(4).myData.name);

workflow.info("****age: " + data.get(4).myData.age);

workflow.info("****car: " + data.get(4).myData.car);

workflow.info("****eye color: " + data.get(4).myData.eye_color);

bsweetser explains how to get at the Databus variables

Hopefully, you see output like this in the Workflow Log:

image

So, there you have it. Passing an object from PowerShell to the workflow, then parsing the data.

View original source

https://www.servicenow.com/community/now-platform-articles/passing-an-object-from-powershell-custom-activity-to-workflow/ta-p/2310611