3

How can I set up a Powershell named pipe to be available to all users on the local computer?

I'm writing a small Powershell script and I need to be able to have any user on the local computer be able to write to a named pipe that will be consumed by this script.

I can get the pipe set up with the following:

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

I'm not sure how to use the System.IO.Pipes.PipeSecurity in conjunction with this.

EDIT: I'm using PS v2 if it matters.

EDIT2:

I've made some progress in getting a named pipe security definition created. Still not sure how to bind it.

$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 ) 
Tim Brigham
  • 574
  • 1
  • 6
  • 24
  • I think you just need to use the overload in `new-object` that takes in a PipeSecurity object. See [this old SO question of mine](http://stackoverflow.com/questions/3478166/named-pipe-server-throws-unauthorizedaccessexception-when-creating-a-seccond-ins) asking about a similar problem in C#, you may be able to translate it in to PS. – Scott Chamberlain Oct 17 '13 at 19:13

1 Answers1

1

You need to use the right constructor, that property can't be set afterwards. This will do the trick:

$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);
Dave
  • 97
  • 1
  • 8
  • Thanks Robert! Note the following line should be included as well before creating the pipe: $PipeSecurity.AddAccessRule($AccessRule) – YasharBahman Nov 30 '15 at 02:15