问题描述
假设我正在运行两个 PowerShell 程序:Producer.ps1
和 Consumer.ps1
.
Let's say I have two PowerShell programs running: Producer.ps1
and Consumer.ps1
.
有没有办法在我拥有的两个 .ps1
文件之间建立客户端-服务器关系?
Is there any way to establish a client-server relationship between the two .ps1
files I have?
具体来说,Producer.ps1
输出一个包含登录信息的 PSObject
.有什么办法可以在两个对象之间建立一个侦听器和命名管道,以将这个 PSObject
从 Producer.ps1
直接传递给 Consumer.ps1
?
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
?
(注意:这两个文件不能合并,因为它们每个都需要以不同的 Windows 用户身份运行.我知道一种可能的通信解决方案是编写 PSObject
到文本/xml 文件,然后让客户端读取并删除该文件,但我宁愿不这样做,因为它会公开凭据.我愿意接受您提出的任何建议)
(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)
推荐答案
我发现此链接描述了如何执行您的请求:https://gbegerow.wordpress.com/2012/04/09/interprocess-communication-in-powershell/
I found this link that describes how to do what you're requesting:https://gbegerow.wordpress.com/2012/04/09/interprocess-communication-in-powershell/
我对其进行了测试,并且能够在两个单独的 Powershell 会话之间传递数据.
I tested it and I was able to pass data between two separate Powershell sessions.
服务器端:
$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();
这篇关于两个单独的 Powershell 进程之间的流水线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!