Skip navigation
Return Multiple Values from Sub-PowerShell Process

Return Multiple Values from Sub-PowerShell Process

Q: How can I return more than one value from Windows PowerShell called from a sub-PowerShell process from Orchestrator?

A: There are many different ways to create the sub-PowerShell processes from Orchestrator's Run.Net script activity, but all of them allow a single value to be returned; for example,

$value = powershell.exe { <your code> }

However, consider in the PowerShell code if there were two or more values that you wanted to return and publish to the data bus. You've got several different approaches possible. The method I prefer is to create a custom object in PowerShell, insert the various values you want to return as properties of the custom object, then extract them back outside of the sub-PowerShell process. For example:

$tmpobj = powershell.exe {

new-object pscustomobject -property @{
version = $PSVersiontable.PSVersion
arch = [intptr]::Size
}
}

$version = $tmpobj.version
$arch = $tmpobj.arch

Notice the components to this: A sub-PowerShell process is created using powershell.exe {, and the result of that PowerShell process is stored in the $tmpobj variable.

Inside the sub-PowerShell process, a custom object is created that has two separate properties added: version and arch.

Outside the sub-PowerShell process, those individual properties are then extracted into new $version and $arch variables, which could then be published to the data bus.

Simple? No, but it works.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish