Powershell EXE script return values

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
jasonv
Posts: 24
Joined: Tue Jul 19, 2016 8:28 am

Powershell EXE script return values

Post by jasonv » Tue May 21, 2019 10:25 am

I'm attempting to create two scripts, scriptA, scriptB. Both are .EXE. ScriptA calls ScriptB as follows.

Code: Select all

$var1 = & ".\scriptb.exe"
Here is scriptB..

Code: Select all

Write-Host "TESTING..."
$Object = New-Object -TypeName PSObject -Property @{'Prop1' = "Test1"; 'Prop2' = "Test2"}

return $Object
If scriptB is a .PS1 file it works fine. The object is return as expected.

If scriptB is an .exe file it just returns everything. I.E. All output and the object

I know that .exe file just returns text, but is there a way to only return some of the text? How is this typically handled? I ended up exporting the object as a .csv file and then imported back into scriptA. Is there a more efficient way to do this rather than creating temp files?

BTW: I did read this...

https://www.sapien.com/blog/2015/12/17/ ... able-file/

Thanks.

User avatar
Alexander Riedel
Posts: 7005
Joined: Tue May 29, 2007 4:43 pm

Re: Powershell EXE script return values

Post by Alexander Riedel » Tue May 21, 2019 10:29 am

If you run two scripts in the same runspace you can hand objects back and forth. If you have two exe files you have two individual processes each with its own runspace. There is no object passing back and forth.
Typically if you need to pass objects you should do that in the same script. Any particular reason for two processes?
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
jasonv
Posts: 24
Joined: Tue Jul 19, 2016 8:28 am

Re: Powershell EXE script return values

Post by jasonv » Tue May 21, 2019 12:22 pm

The script started out as a single script, but the source data for each was so disconnected that I needed to call one from the other. I may end up running them separately, but the second one depends on the first one completing. May need to create an until loop and detect the process completion or something to that affect.

Thanks for the quick response.

Locked