2013-10-17 114 views
3

如何设置一个Powershell命名管道供本地计算机上的所有用户使用?Powershell命名管道安全?

我正在编写一个小的PowerShell脚本,我需要能够让本地计算机上的任何用户能够写入将由该脚本使用的命名管道。

我可以得到管道设置了以下内容:

$pipe=new-object System.IO.Pipes.NamedPipeServerStream("\\.\pipe\mypipe") 
$pipe.WaitForConnection() 
$sr = new-object System.IO.StreamReader($pipe) 
while (($cmd= $sr.ReadLine()) -ne 'exit') 
.... 

我不知道如何与这个使用System.IO.Pipes.PipeSecurity结合。

编辑:我使用PS v2,如果它很重要。

EDIT2:

我做的越来越创建了一个名为管道安全定义了一些进展。仍然不确定如何绑定它。

$PipeSecurity = new-object System.IO.Pipes.PipeSecurity 
$AccessRule = New-Object System.IO.Pipes.PipeAccessRule("Users", "FullControl", "Allow") 
$PipeSecurity.AddAccessRule($AccessRule) 
$pipe=new-object System.IO.Pipes.NamedPipeServerStream("\\.\pipe\mypipe"); 
$pipe.SetAccessControl($PipeSecurity) 
+0

我认为你只需要在'new-object'中使用接受PipeSecurity对象的重载。请参阅[这是我的老问题](http://stackoverflow.com/questions/3478166/named-pipe-server-throws-unauthorizedaccessexception-when-creating-a-seccond-ins)问在C#中的类似问题,你可能会把它翻译成PS。 –

回答

1

您需要使用正确的构造函数,该属性不能在事后设置。这将做诡计:

$PipeSecurity = new-object System.IO.Pipes.PipeSecurity 
$AccessRule = New-Object System.IO.Pipes.PipeAccessRule("Users", "FullControl", "Allow") 
$PipeSecurity.AddAccessRule($AccessRule) 
$pipe=new-object System.IO.Pipes.NamedPipeServerStream("p","In",100, "Byte", "Asynchronous", 32768, 32768, $PipeSecurity); 
+0

谢谢罗伯特!注意,在创建管道之前,还应该包含以下行:$ PipeSecurity.AddAccessRule($ AccessRule) – YasharBahman