Skip to content

Instantly share code, notes, and snippets.

@joegasper
Last active July 8, 2024 22:13
Show Gist options
  • Save joegasper/8b0f81239160d30181e4fe53d196df1c to your computer and use it in GitHub Desktop.
Save joegasper/8b0f81239160d30181e4fe53d196df1c to your computer and use it in GitHub Desktop.
Send TCP Message in PowerShell
<#
.SYNOPSIS
Sends an IP message (TCP or UDP) to a specified endpoint and port.
.DESCRIPTION
The Send-IpMessage function sends a message over the specified IP protocol (TCP or UDP) to a specified endpoint and port.
It supports sending messages with or without a CRLF (Carriage Return Line Feed) at the end of the message.
.PARAMETER EndPoint
The endpoint (IP address or hostname) to which the message will be sent.
.PARAMETER Port
The port number on the endpoint to which the message will be sent.
.PARAMETER Message
The message to be sent to the specified endpoint and port.
.PARAMETER WithCrLf
If specified, the message will be sent with appending a CRLF at the end. By default, no CRLF is appended.
.PARAMETER Protocol
The protocol to use for sending the message. Valid options are "TCP" and "UDP". Default is "TCP".
.EXAMPLE
Send-IpMessage -EndPoint '192.168.1.1' -Port 80 -Message 'Hello, World' -Protocol TCP
Sends "Hello, World" to the endpoint 192.168.1.1 on port 80 using TCP.
.EXAMPLE
Send-IpMessage -EndPoint '192.168.1.1' -Port 80 -Message 'Hello, World' -Protocol UDP
Sends "Hello, World" to the endpoint 192.168.1.1 on port 80 using UDP.
.NOTES
Adapted from https://riptutorial.com/powershell/example/18117/tcp-listener for both TCP and UDP communication.
#>
Function Send-IpMessage {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string] $EndPoint,
[Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)]
[int] $Port,
[Parameter(Position = 2)]
[ValidateSet("TCP", "UDP")]
[string] $Protocol = "TCP",
[Parameter(Mandatory = $true, Position = 3, ValueFromPipeline = $true)]
[string] $Message,
[Parameter(Position = 4)]
[switch] $WithCrLf
)
Process {
Write-Verbose "Preparing message for sending to ${EndPoint}:${Port}..."
$CrLf = if ($WithCrLf.IsPresent) { "`r`n" } else { "" }
$EncodedMessage = [System.Text.Encoding]::ASCII.GetBytes($Message + $CrLf)
Write-Verbose "Message encoded. Length: $($EncodedMessage.Length) bytes."
if ($Protocol -eq "TCP") {
Write-Verbose 'Using TCP protocol.'
$Client = New-Object System.Net.Sockets.TcpClient($EndPoint, $Port)
$Stream = $Client.GetStream()
$Stream.Write($EncodedMessage, 0, $EncodedMessage.Length) | Out-Null
Write-Verbose 'Message sent.'
$Stream.Close()
$Client.Close()
}
elseif ($Protocol -eq "UDP") {
Write-Verbose 'Using UDP protocol.'
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Send($EncodedMessage, $EncodedMessage.Length, $EndPoint, $Port) | Out-Null
Write-Verbose 'Message sent.'
$UdpClient.Close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment