两个单独的 Powershell 进程之间的流水线

Pipelining between two SEPARATE Powershell processes

假设我有两个 PowerShell 程序 运行ning:Producer.ps1Consumer.ps1

有没有办法在我拥有的两个 .ps1 文件之间建立客户端-服务器关系?

具体来说,Producer.ps1 输出包含登录信息的 PSObject。有什么办法可以在两个对象之间建立 侦听器和命名管道 以将此 PSObjectProducer.ps1 直接传递到 Consumer.ps1


(注意:这两个文件不能合并,因为它们每个都需要 运行 作为不同的 Windows 用户。我知道一个可能的解决方案通信是将 PSObject 写入 text/xml 文件,然后让客户端读取并删除该文件,但我宁愿不这样做,因为它会公开凭据。我愿意接受任何建议你有)

我发现这个 link 描述了如何执行您的请求: https://gbegerow.wordpress.com/2012/04/09/interprocess-communication-in-powershell/

我对其进行了测试,我能够在两个单独的 Powershell 会话之间传递数据。

服务器端:

$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();

客户端:

$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();