Powershell EXE script return values

Ask your 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.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jasonv
Posts: 49
Last visit: Thu Sep 01, 2022 5:40 am

Powershell EXE script return values

Post by jasonv »

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: 8488
Last visit: Mon Apr 15, 2024 3:28 pm
Answers: 20
Been upvoted: 37 times

Re: Powershell EXE script return values

Post by Alexander Riedel »

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: 49
Last visit: Thu Sep 01, 2022 5:40 am

Re: Powershell EXE script return values

Post by jasonv »

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.
This topic is 4 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked