1

I want to create a PowerShell script that will be running in the background as some kind of service, let's name it BackEnd.ps1. An additional script (e.g. FrontEnd.ps1) will be started on whenever needed by the user.

The FrontEnd.ps1 script should be able to read / write variables in the BackEnd.ps1 script.

But how could I realize this? Is this possible via Enter-PSHostProcess cmdlet? Enter-PSSession seems not to work for me since I don't know how to retrieve the session from the BackEnd.ps1.

Edit: The question Pipelining between two SEPARATE Powershell processes is not quiet similar to mine. Contrary to that one I would like to exchange objects between the scripts as well. The solution with the pipe only allows to exchange strings.

GuidoT
  • 280
  • 1
  • 12
  • I believe I have an answer to your question, but your question is locked to where it is no longer accepting answers. Could you either re-enable answers on this one, or if that can't be done then ask a new question that explicitly ask for passing objects between scripts? – Darin Mar 20 '23 at 18:46
  • To reopen the question, you might try following the instructions [here](https://meta.stackoverflow.com/a/252254/4190564). If you change the title, or create a new question, the title should probably be something like "Passing objects between PowerShell scripts" – Darin Mar 20 '23 at 20:31
  • Thank you for your advice. I changed the title. – GuidoT Mar 22 '23 at 08:40
  • @Darin Could you share your answer please? My question was reopened. :-) – GuidoT Mar 27 '23 at 10:46
  • Yes, I will try to get an answer put together in sometime today. – Darin Mar 27 '23 at 15:27
  • I am working on this, but I need to know more about your BackEnd. The method that I got to work was to use NamedPipeServerStream and NamedPipeClientStream (sending objects as text, converting them back to objects when received). The easiest way to do this is to use WaitForConnection, but this will freeze the BackEnd's thread and the BackEnd will sit and wait, doing nothing. But if you need it to be doing other things while waiting, then that requires WaitForConnectionAsync, which I haven't tried yet. But I think is doable. – Darin Mar 28 '23 at 02:36
  • Also, just found out that communication between two user accounts requires an extra step or two. So, if you are running your backend with an admin account, while the client is ran by an ordinary user, then this is required. How are you starting the backend? Task Scheduler with local Admin account? – Darin Mar 28 '23 at 11:19

0 Answers0