PowerShell/Scripts/send-udp.ps1

35 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-08-26 10:46:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
send-udp.ps1 [<target-IP>] [<target-port>] [<message>]
.DESCRIPTION
2021-08-29 17:50:03 +02:00
Sends a UDP datagram message to the given IP address and port.
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\send-udp.ps1 192.168.100.100 8080 "TEST"
2021-08-29 17:50:03 +02:00
.NOTES
Author: Markus Fleschutz · License: CC0
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2020-12-29 15:14:21 +01:00
#>
2020-11-28 09:18:36 +01:00
2021-07-15 15:51:22 +02:00
param([string]$TargetIP = "", [int]$TargetPort = 0, $[string]Message = "")
2020-11-28 09:18:36 +01:00
2020-11-28 09:43:13 +01:00
try {
2021-07-15 15:51:22 +02:00
if ($TargetIP -eq "" ) { $TargetIP = read-host "Enter target IP address" }
if ($TargetPort -eq 0 ) { $TargetPort = read-host "Enter target port" }
if ($Message -eq "" ) { $Message = read-host "Enter message to send" }
2020-11-28 09:43:13 +01:00
$IP = [System.Net.Dns]::GetHostAddresses($TargetIP)
$Address = [System.Net.IPAddress]::Parse($IP)
$EndPoints = New-Object System.Net.IPEndPoint($Address, $TargetPort)
$Socket = New-Object System.Net.Sockets.UDPClient
$EncodedText = [Text.Encoding]::ASCII.GetBytes($Message)
$SendMessage = $Socket.Send($EncodedText, $EncodedText.Length, $EndPoints)
$Socket.Close()
2021-08-24 20:46:03 +02:00
"✔️ Done."
2020-11-28 09:43:13 +01:00
exit 0
2020-12-09 10:30:55 +01:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2020-12-09 10:30:55 +01:00
exit 1
}