Return Multiple Values from Sub-PowerShell Process

How to return multiple values back from a sub-PowerShell process.

John Savill

October 14, 2013

1 Min Read
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 {  }

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.PSVersionarch = [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.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like