2013-10-17 112 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