Public/New-PSNamedPipeServer.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
<# .SYNOPSIS Creates a new Named Pipe Server .DESCRIPTION Creates a new NamedPipe Server that can send or receive communications from a client .EXAMPLE Local machine NamedPipe Server Message Mode Create NamedPipe Server on the local machine that can send communications to the client in Message mode New-PSNamedPipeServer -Name 'ps-namedpipe-server' -ComputerName '.' -Direction Out -MaxInstances 1 -Mode Message .EXAMPLE Local machine NamedPipe Server Byte Mode Create NamedPipe Server on the local machine that can send communications to the client in Byte mode New-PSNamedPipeServer -Name 'ps-namedpipe-server' -ComputerName '.' -Direction Out -MaxInstances 1 -Mode Byte .EXAMPLE Remote machine NamedPipe Server Message Mode Create NamedPipe Server on a remote machine that can send communications to the client in Message mode New-PSNamedPipeServer -Name 'ps-namedpipe-server' -ComputerName 'DESKTOP-12345' -Direction Out -MaxInstances 1 -Mode Message .EXAMPLE Remote machine NamedPipe Server Byte Mode Create NamedPipe Server on a remote machine that can send communications to the client in Byte mode New-PSNamedPipeServer -Name 'ps-namedpipe-server' -ComputerName 'DESKTOP-12345' -Direction Out -MaxInstances 1 -Mode Byte #> function New-PSNamedPipeServer { [CmdletBinding(DefaultParameterSetName = 'Parameter Set 1', PositionalBinding = $false, HelpUri = 'http://www.microsoft.com/', ConfirmImpact = 'Medium')] [Alias()] [OutputType([String])] Param ( # Param1 help description [Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)] [string]$Name, # Param2 help description [Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)] [string]$ComputerName = '.', # Param3 help description [Parameter(Mandatory = $true, Position = 2, ValueFromPipelineByPropertyName = $true)] [ValidateSet("In", "Out", "InOut")] $Direction, [Parameter(Mandatory = $true, Position = 3, ValueFromPipelineByPropertyName = $true)] [int] $MaxInstances = 1, [Parameter(Mandatory = $true, Position = 4, ValueFromPipelineByPropertyName = $true)] [ValidateSet("Byte", "Message")] $Mode ) Write-Verbose -Message 'Create NamedPipe Server connection' try { $pipeServer = New-Object -TypeName System.IO.Pipes.NamedPipeServerStream -ArgumentList @( $Name, $Direction, $MaxInstances, $Mode) } catch { Write-Error -ErrorRecord $Error[0] } Write-Debug -Message 'Waiting for connection to NamedPipe server' $pipeServer.WaitForConnection() # Write data to the pipe, to be read by the client. $writer = New-Object -TypeName System.IO.StreamWriter -ArgumentList @($pipeServer) $writer.AutoFlush = $true #Flush buffer to stream after every Write(). $writer.Write("This string was read from the pipe named " + $Name) # Now remove the named pipe and clean up. $pipeServer.Close() } |