NamedPipes.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
<#
.Synopsis
    Sends a message of a named pipe.
.DESCRIPTION
    Sends a message of a named pipe.This named pipe can exist locally or on a remote machine. By default,
    this cmdlet sends the message using Unicode encoding.
.EXAMPLE
   Send-NamedPipeMessage -PipeName "DrainPipe" -ComputerName "domaincontroller" -Message "Screw you!"
.EXAMPLE
   Send-NamedPipeMessage -PipeName "SewerPipe" -Message "Hello, Pipe!"
#>

function Send-NamedPipeMessage
{
    param(
    # The named pipe to send the message on.
    [String]$PipeName,
    # The computer the named pipe exists on.
    [String]$ComputerName=".",
    # The message to send the named pipe on.
    [string]$Message,
    # The type of encoding to encode the string with
    [System.Text.Encoding]$Encoding = [System.Text.Encoding]::Unicode,
    # The number of milliseconds before the connection times out
    [int]$ConnectTimeout = 5000
    )

    $stream = New-Object -TypeName System.IO.Pipes.NamedPipeClientStream -ArgumentList $ComputerName,$PipeName,([System.IO.Pipes.PipeDirection]::Out), ([System.IO.Pipes.PipeOptions]::None),([System.Security.Principal.TokenImpersonationLevel]::Impersonation)
    $stream.Connect($ConnectTimeout)

    $bRequest = $Encoding.GetBytes($Message)
    $cbRequest = $bRequest.Length; 
 
    $stream.Write($bRequest, 0, $cbRequest); 

    $stream.Dispose()
}