How to pass JSON to PowerShell MID server script

chadlockwood
Kilo Sage

I am trying to pass JSON from my workflow to a PowerShell MID server script through a Run Powershell activity. The JSON string is being created in a Run Script activity:

var dataObj = {};

dataObj.displayName = workflow.scratchpad.displayName;

dataObj.employeeNumber = workflow.scratchpad.employeeNumber;

dataObj.manager = workflow.scratchpad.manager;

dataObj.title = workflow.scratchpad.title;

workflow.scratchpad.employeeJSON = new global.JSON().encode(dataObj);

I then enter the following in the Powershell script variables field on the activity:

{

      "scratchpadData":'${workflow.scratchpad.employeeJSON}'

}

The PowerShell script is this (for testing purposes only):

$psVariable = $env:SNC_scratchpadData | ConvertFrom-JSON

Write-Host $psVariable

The output I would expect to see:

{"displayName":"Carl Plowman","employeeNumber":"12345","manager":"Duffy McBean","title":"Overlord II"}

Instead I get an error from the activity:

*** Invalid JSON primitive: object.Stack Trace:       at

System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()at

System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth)at

System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit,

JavaScriptSerializer serializer)at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer

serializer, String input, Type type, Int32 depthLimit)at

Microsoft.PowerShell.Commands.ConvertFromJsonCommand.ProcessRecord()at

System.Management.Automation.CommandProcessor.ProcessRecord()

I suspect this has to do with the fact that when I enter the JSON into the PowerShell script variables field, it effectively becomes JSON inside of JSON. When I change the PowerShell script to simply print out the data that has been passed:

Write-Host $env:SNC_scratchpadData

I see this in the activity output:

[object Object]

If I eliminate the MID server script file and write similar PowerShell code directly into the Command field in the activity, and pass the workflow scratchpad variable directly to the script:

Write-Host '${workflow.scratchpad.employeeJSON}'

I get the expected output:

{"department":"123","displayName":"Carl Plowman","employeeNumber":"12345","manager":"Duffy McBean","title":"Overlord II"}

I am looking for assistance in either encapsulating the JSON string from the workflow in a manner that it is useable by the MID server script file -OR- handling what I am currently sending to the MID server script file in a way that makes it useful.

Thank you for your time,

Chad Lockwood

1 ACCEPTED SOLUTION

chadlockwood
Kilo Sage

Ok. We got this sorted out.



The PowerShell script variables field on the Run Powershell activity is expecting "Additional parameters, in JSON format". In all previous workflows I've created, I did the following:


{


  "key name":"${workflow.scratchpad.key value}"


}


The scratchpad variable had always been a string so this worked.



My workflow.scratchpad.employeeJSON variable was configured as a JSON string before I put it in the PowerShell script variables field, so it did not require the open and close braces -OR- the additional key name. My scratchpad variable value was:



{"department":"123","displayName":"Carl Plowman","employeeNumber":"12345","manager":"Duffy McBean","title":"Overlord II"}


If I entered just ${workflow.scratchpad.employeeJSON} into the PowerShell script variables field, in my PowerShell code I can do this:


Write-Host $env:SNC_displayName


and the output is Carl Plowman



I'm please with this result as it allows me to pass a full JSON string, created from JavaScript, into PowerShell.



I hope this helps if anyone else is running into this issue.



Chad


View solution in original post

1 REPLY 1

chadlockwood
Kilo Sage

Ok. We got this sorted out.



The PowerShell script variables field on the Run Powershell activity is expecting "Additional parameters, in JSON format". In all previous workflows I've created, I did the following:


{


  "key name":"${workflow.scratchpad.key value}"


}


The scratchpad variable had always been a string so this worked.



My workflow.scratchpad.employeeJSON variable was configured as a JSON string before I put it in the PowerShell script variables field, so it did not require the open and close braces -OR- the additional key name. My scratchpad variable value was:



{"department":"123","displayName":"Carl Plowman","employeeNumber":"12345","manager":"Duffy McBean","title":"Overlord II"}


If I entered just ${workflow.scratchpad.employeeJSON} into the PowerShell script variables field, in my PowerShell code I can do this:


Write-Host $env:SNC_displayName


and the output is Carl Plowman



I'm please with this result as it allows me to pass a full JSON string, created from JavaScript, into PowerShell.



I hope this helps if anyone else is running into this issue.



Chad