Returning Data from a Remote PowerShell execution in Orchestrator

A question came up this week internally about how to return data from a remotely executed script (a PowerShell script inside of a Run .NET Script object in Orchestrator 2012).

I am trying to remotely execute a script using the Orchestrator .Net Script Object. I have it set up like so (screenshot) and the returned data doesn’t come back. The $date and $fileName property I have set up as returned data and they are both blank, I tried moving them outside the Invoke-Command and set them inside, no go. Any ideas?

This was actually answered by another person on my team and I thought I would share that answer out!

Orchestrator won’t be able to publish those values to the data bus because those variables only exist inside your PowerShell session on the remote box. If you need to publish data from a remote script execution to the database you need to “bring it back” to the local PowerShell session.

The way I’ve been able to do this in the past is to create a PSSession for executing the remote commands. Then assign the values you want to return to variables within the remote PSSession like you are already doing. Then do separate Invoke-Commands to return each variable that you need. In those invoke commands just include the variable name (e.g. “$fileName) and the value returned by the Invoke-Command should be the value of the variable. This is convoluted so I tried to express it in pseudo code below. In the example localResult is the name of the variable you would publish to the SCOrch data bus.

$session = New-PSSession -credential $creds -ComputerName $host
Invoke-Command -session $session -ScriptBlock {
#do stuff here
$remoteResult = Get-Process;

$localResult = Invoke-Command -session $session -ScriptBlock {$remoteResult}

Soon after posting this I received some suggestions by Stijn Callebaut from inovativ discussing some additional improvements

The proposed solution (Above) This reads as:
1. Create the remote session
2. Run the get-process command on the remote system en store the result in $remoteresult
3. Invoke another command on the remote system, returning the result.
This can be achieved a lot easier.

$session = New-PSSession -credential $creds -ComputerName $host
$result = Invoke-Command -session $session -ScriptBlock {
#do stuff here
$remoteResult = Get-Process

Or (even easier):

$session = New-PSSession -credential $creds -ComputerName $host
$result = Invoke-Command -session $session -ScriptBlock {
#do stuff here

$result, would be defined as a return value of the activity. Now we have some process object captured in our result variable. When you want to return multiple objects, for example the services and the processes that run on the remote system it becomes a little bit more complicated.

You might think that the following could work, but it doesn’t (it actually does, but the output is worthless if using in orchestrator). The result is actually an array containing different items of type System.object. Each object with its own properties.

$session = New-PSSession -credential $creds -ComputerName $host
$result = Invoke-Command -session $session -ScriptBlock {
#do stuff here

So how to get this working in Orchestrator and actually returning multiple outputs in the Run .Net activity?

$session = New-PSSession -credential $creds -ComputerName $host
$result = Invoke-Command -session $session -ScriptBlock {
#do stuff here
new-object pscustomobject –property @{
pc = $env:computername
domain = $env:userdomain
$pc = $result.pc
$domain = $result.domain

The pc and domain variables can be defined as output from the Run .Net Activity.

In case of returning array’s this still cannot be used in Orchestrator if you want this to be available on the data bus. Remember that orchestrator output data needs to be flattened. Also output types are limited.

As this is another topic and Charles Joy already described this very well, covering all possible scenarios, I’ll just refer to his blog post.

Hope it helps,

Thanks for the thoughts Stijn!

Tagged with: , , ,
Posted in Workflow
3 comments on “Returning Data from a Remote PowerShell execution in Orchestrator
  1. STU says:

    Great Article! I struggled with the empty variables issue for a whole day, until I read the post.

  2. Falken says:

    Thanks for the article.

    I struggle with this when it’s inside a function. Can’t i do the pscustomobject inside a function and output to the databus?

  3. Noah Stahl says:

    I’ve used this technique often and ended up creating a reusable template for making it easier. Feel free to download and learn more:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: