-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend.ps1
41 lines (35 loc) · 1.08 KB
/
send.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
Function Send-TCPMessage {
Param (
[Parameter(Mandatory=$true, Position=1)]
[ValidateNotNullOrEmpty()]
[string]
$EndPoint
,
[Parameter(Mandatory=$true, Position=0)]
[int]
$Port
,
[Parameter(Mandatory=$true, Position=2)]
[string]
$Message
)
Process {
# Setup connection
$IP = [System.Net.Dns]::GetHostAddresses($EndPoint)
$Address = [System.Net.IPAddress]::Parse($IP)
$Socket = New-Object System.Net.Sockets.TCPClient($Address,$Port)
# Setup stream wrtier
$Stream = $Socket.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
# Write message to stream
$Message | % {
$Writer.WriteLine($_)
$Writer.Flush()
}
# Close connection and stream
$Stream.Close()
$Socket.Close()
}
}
#ip jesu : 192.168.137.40
#Send-TCPMessage -Port 29800 -Endpoint 192.168.0.1 -message "My first TCP message !"