Processing PowerShell Script Result by Orchestrator

Share on:

Collecting and parsing PowerShell script result by Aria Automation Orchestrator.

Subject

Recently I've been tasked to automate existing PowerShell script running. The scripts manage email messages on an Exchange Online server and I needed to collect and send the script output. I started to experiment with Orchestartor PowerShell plugin and came up with a solution presented below.

Open (and reuse) a session

In case you run multiple scripts on a single PowerShell host, it is worth to open a session and run the commands via this single session. This will save a lot of time, as creating a session can be time-comsuming (compared to the commands/scripts run on the host). The first sample workflow Open Session demonstrates the point. It runs two simple scripts, the first is the whoami command, and second is listing the local groups defined: Get-LocalGroup | Select-Object Name,SID. The test run is below: it took 5 seconds to build the connection, and less than a second to run each command:

Getting the output

The easiest way of getting the result is getting the host output (used in the Open Session workflow):

1var session = psHost.getSession(sessionId);
2
3var result = session.invokeScript(script1);
4
5System.log("Script run: " + result.getInvocationState());
6System.log("Host output:\n" + result.getHostOutput());
7var errors = result.getErrors();
8if (errors.length) System.error("Script errors:\n" + errors.join('\n'));

There is a catch with this approach: each command can have a different output format, and we rely on string processing to gather the results. Here are the output of running the two scripts above:

Get Results as JSON

This workflow use the getResults() function of the PowerShellInvocationResult object. This function returns a PowerShellRemotePSObject. This object has a getAsJson() function.

1if ("Completed" == result.getInvocationState()) {
2    var json = result.getResults().getAsJson();
3    var resultObj = JSON.parse(json);
4    System.log(JSON.stringify(resultObj, null, 2));
5}

Let's see what is the return value of this function for the following script: Get-LocalGroup -Name Guests; Get-LocalUser Guest:

Does not seem to be easily parsed, unfortunately.

Get Results as XML

Let's try the XML output: what could possibly go wrong?

1if ("Completed" == result.getInvocationState()) {
2    var xml = result.getResults().getXml();
3    var xmlObject = new XML(xml);
4    System.log(xmlObject);
5}

This is the same complex structure in XML. Hard to find the exact information we need within:

Hair of the dog: XML to JSON

Let's process the the PowerShell script result by PowerShell!
Orchestrator has built-in PowerShell engine, and luckily we have the DeSerialize method in PSSerializer Class to deserialize a PowerShell CliXml into an object. Then we convert the object to JSON and collect the result:

 1function Handler($context, $inputs) {
 2    $xml = $inputs.xml
 3    Write-Host "XML input length:" $xml.Length
 4
 5    try { $json = [System.Management.Automation.PSSerializer]::DeSerialize($xml) | ConvertTo-Json -Compress }
 6    catch {Write-Error $_; $json = '{}'}
 7
 8    $output = @{json = $json}
 9    return $output
10}

The collected result can be easily used:

Wrap up

PowerShell script result can be a complex object, and parsing the JSON/XML representations of it is complex. The method above provides a simple way to do that.

Bear in mind that the polyglot PowerShell engine is available only within Orchestrator intances licenced with Aria Automation.

You can find the sample workflows at GitHub: https://github.com/kuklis/vro8-packages
Package com.test.powershell