Public/New-PSNamedPipeClient.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 83 84 85 86 87 88 |
<# .SYNOPSIS Creates a new Named Pipe client .DESCRIPTION Creates a new NamedPipe client that can send or receive communications from a NamedPipe Server .EXAMPLE Local machine NamedPipe Server Message Mode Create NamedPipe client on the local machine that can send communications to the server in Message mode New-PSNamedPipeServer -Name 'ps-namedpipe-server' -ComputerName '.' -Direction InOut .EXAMPLE Local machine NamedPipe Client Byte Mode Create NamedPipe client on the local machine that can send communications to the server in Byte mode New-PSNamedPipeServer -Name 'ps-namedpipe-server' -ComputerName '.' -Direction InOut .EXAMPLE Remote machine NamedPipe Client Message Mode Create NamedPipe client on this machine and send communications to the remote NamedPipe server in Message mode New-PSNamedPipeServer -Name 'ps-namedpipe-server' -ComputerName 'DESKTOP-12345' -Direction InOut .EXAMPLE Remote machine NamedPipe client Byte Mode Create NamedPipe client on this machine and send communications to the remote NamedPipe server in Byte mode New-PSNamedPipeServer -Name 'ps-namedpipe-server' -ComputerName 'DESKTOP-12345' -Direction InOut #> function New-PSNamedPipeClient { [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 ) Write-Verbose -Message 'Creating NamedPipe Client stream' try { $pipeClient = New-Object -TypeName System.IO.Pipes.NamedPipeClientStream -ArgumentList @( $ComputerName, $Name, $Direction) } catch { Write-Error -ErrorRecord $Error[0] } Write-Verbose -Message 'Creating connection' try { $pipeClient.Connect() $pipeClient.ReadMode = [System.IO.Pipes.PipeTransmissionMode]::Message $pipeBuffer = New-Object -TypeName System.Byte[] -ArgumentList 10000 } catch { Write-Error -ErrorRecord $Error[0] } Write-Debug -Message 'Reading from the NamedPipe Server' # read from the pipe $pipeCounter = $pipeClient.Read($pipeBuffer, 0, $pipeBuffer.count) Write-Debug -Message 'Trimming the buffer array' # Trim off the unfilled portion of the buffer array. $pipeBuffer = $pipeBuffer[0..($pipeCounter - 1)] # Cast byte array into a character array, join into a single string, then print. [char[]] $pipeBuffer -join "" ## Optionally, you can read data from the pipe byte-by-byte instead. # $pipeclient.ReadMode = [System.IO.Pipes.PipeTransmissionMode]::Byte # $reader = new-object System.IO.StreamReader -arg @($pipeclient) # while (($temp = $reader.ReadLine()) -ne $null) { $temp + "`n" } } |