6

Let's say I have two PowerShell programs running: Producer.ps1 and Consumer.ps1.

Is there any way to establish a client-server relationship between the two .ps1 files I have?

Specifically, Producer.ps1 outputs a PSObject containing login info. Is there any way I can establish a listener and named pipe between the two objects to pass this PSObject from Producer.ps1 directly into Consumer.ps1?


(Note: the two files cannot be combined, because they each need to run as different Windows users. I know one possible solution for communicating is to write the PSObject to a text/xml file, then have the client read and erase the file, but I'd rather not do this since it exposes the credentials. I'm open to whatever suggestions you have)

JayFresco
  • 295
  • 1
  • 3
  • 11

1 Answers1

8

I found this link that describes how to do what you're requesting: https://gbegerow.wordpress.com/2012/04/09/interprocess-communication-in-powershell/

I tested it and I was able to pass data between two separate Powershell sessions.

Server Side:

$pipe=new-object System.IO.Pipes.NamedPipeServerStream("\\.\pipe\Wulf");
'Created server side of "\\.\pipe\Wulf"'
$pipe.WaitForConnection(); 

$sr = new-object System.IO.StreamReader($pipe); 
while (($cmd= $sr.ReadLine()) -ne 'exit') 
{
 $cmd
}; 

$sr.Dispose();
$pipe.Dispose();

Client Side:

$pipe = new-object System.IO.Pipes.NamedPipeClientStream("\\.\pipe\Wulf");
 $pipe.Connect(); 

$sw = new-object System.IO.StreamWriter($pipe);
$sw.WriteLine("Go"); 
$sw.WriteLine("start abc 123"); 
$sw.WriteLine('exit'); 

$sw.Dispose(); 
$pipe.Dispose();
William Nelson
  • 640
  • 1
  • 4
  • 14
  • This is a great find and exactly what I'm looking to do. Thanks a ton! – JayFresco Aug 17 '15 at 18:29
  • 3
    Clarification for future readers: **Server Side** is the listener, and **Client Side** is the producer. In this case, the client is sending the message, through the pipe, to the server. – JayFresco Aug 17 '15 at 18:42