-
Notifications
You must be signed in to change notification settings - Fork 0
/
uA-StartTcpReceiver.ps1
138 lines (113 loc) · 4.23 KB
/
uA-StartTcpReceiver.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<#
.SYNOPSIS
Starts a TCP listener and captures incoming data to a file until a timeout is reached or a specific string is found.
.DESCRIPTION
This script starts a TCP listener on a specified port and captures incoming data to a file until a specific string is found or a timeout is reached. It uses `Start-Transcript` for logging.
.PARAMETER Port
The port number to listen on. Default is 19500.
.PARAMETER TimeoutMinutes
The timeout period in minutes. Set to 0 to disable the timeout. Default is 1 minute.
.PARAMETER SearchString
The specific string to look for in incoming data. Leave empty to disable this check.
.PARAMETER Output
The path to the output file where received data will be written. Default is uA-Data.txt in the script directory.
.PARAMETER LogPath
The path to the log file. Default is $PSCommandPath.txt. This is the full script path with .txt extension.
.PARAMETER EnableLogging
Switch to either enable or disable logging using Start-Transcript. Default is off.
.EXAMPLE
uA-StartTcpReceiver -Port 19501 -TimeoutMinutes 10 -EnableLogging
.EXAMPLE
uA-StartTcpReceiver -TimeoutMinutes 0 -SearchString "sourcetype=uberAgent:Application:Errors" -EnableLogging
.LINK
https://github.com/vastlimits/uberAgentSupport-Scripts
uberagent.com
#>
param (
[int]$Port = 19500,
[int]$TimeoutMinutes = 1,
[string]$SearchString = "",
[string]$Output = "$PSScriptRoot\uA-Data.txt",
[string]$LogPath = "$PSCommandPath.txt",
[switch]$EnableLogging
)
function Start-TcpReceiver {
param (
[int]$Port,
[int]$TimeoutMinutes,
[string]$SearchString,
[string]$Output,
[string]$LogPath,
[switch]$EnableLogging
)
# Initialize logging and variables
if ($EnableLogging) {
Start-Transcript -Path $LogPath -Append
}
$TimeoutReached = $false
$TcpListener = $null
try {
# Check if the listener is already running
if ($TcpListener -eq $null) {
$IPEndPoint = New-Object System.Net.IPEndPoint([IPAddress]::Any, $Port)
$TcpListener = New-Object System.Net.Sockets.TcpListener $IPEndPoint
}
# Attempt to start the listener, handling exceptions if the port is already in use
try {
Write-Host "Starting TCP listener on Port $Port."
$TcpListener.Start()
}
catch {
Write-Host "Error starting listener: $_"
return
}
while (-not $TimeoutReached) {
if ($TcpListener.Pending()) {
$StartTime = Get-Date
$AcceptTcpClient = $TcpListener.AcceptTcpClient()
$GetStream = $AcceptTcpClient.GetStream()
$StreamReader = New-Object System.IO.StreamReader $GetStream
$FileStream = [System.IO.StreamWriter]::new($Output)
Write-Host "Timeout (minutes): $TimeoutMinutes"
Write-Host "Search string: $SearchString"
while ($true) {
$ReadLine = $StreamReader.ReadLine()
$FileStream.WriteLine($ReadLine)
# Check for specific string match (if not disabled)
if ($SearchString -ne "" -and $ReadLine -match $SearchString) {
Write-Host "String '$SearchString' found. Exiting."
$TimeoutReached = $true
break
}
# Check for timeout (if not disabled)
if ($TimeoutMinutes -gt 0) {
$ElapsedMinutes = (Get-Date).Subtract($StartTime).TotalMinutes
if ($ElapsedMinutes -ge $TimeoutMinutes) {
Write-Host "Timeout reached ($TimeoutMinutes minutes). Exiting."
$TimeoutReached = $true
break
}
}
}
# Dispose of resources
$FileStream.Close()
$StreamReader.Dispose()
$GetStream.Dispose()
$AcceptTcpClient.Close()
}
}
}
catch {
Write-Host "An error occurred: $_"
}
finally {
# Stop listening and logging
if ($TcpListener -ne $null) {
$TcpListener.Stop()
}
if ($EnableLogging) {
Stop-Transcript
}
}
}
Start-TcpReceiver -Port $Port -TimeoutMinutes $TimeoutMinutes -SearchString $SearchString -Output $Output -LogPath $LogPath -EnableLogging:$EnableLogging