-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsPortActive.ps1
320 lines (275 loc) · 9.74 KB
/
IsPortActive.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<#PSScriptInfo
.VERSION 1.0.0
.GUID 7ec65ff2-79a3-4ff1-be3c-2dc6b6a3a3d7
.AUTHOR asheroto
.COMPANYNAME asheroto
.TAGS PowerShell Windows port active network check program listening
.RELEASENOTES
[Version 0.0.1] - Initial Release.
[Version 0.0.2] - Removed function.
[Version 0.0.3] - Improved description.
[Version 0.0.4] - Fix example.
[Version 0.0.5] - Added code signing cert.
[Version 1.0.0] - Totally refactored script. Added support for UDP ports. Added support for checking by process name and process ID. Added support for returning service/display name if it's svchost. Added -Help, -CheckForUpdate, and -Version.
#>
<#
.SYNOPSIS
Checks if a port is currently active or in use.
.DESCRIPTION
This script checks if a port is currently active or in use. It checks for active TCP and UDP connections on a specified port, by process name, or by a process ID.
If the process name is svchost, the script will find the service and display name in addition to the process name.
Here are key switches to guide the script's behavior:
-Port to specify the port number to check for active connections.
-ProcessName to specify the name of the process to check for active connections.
-ProcessId to specify the process ID to check for active connections.
Additional utilities include:
-Version switch displays the current version of the script.
-Help switch brings up the help information for the script usage.
-CheckForUpdate switch verifies if the current script version is up-to-date.
.PARAMETER Port
Specifies the port number to check for active connections.
.PARAMETER ProcessName
Specifies the name of the process to check for active connections.
.PARAMETER ProcessId
Specifies the process ID to check for active connections.
.PARAMETER Version
Displays the version of the script.
.PARAMETER Help
Displays the help information for the script.
.PARAMETER CheckForUpdate
Checks for updates of the script.
.EXAMPLE
# Check if port 443 is active
IsPortActive -Port 443
.EXAMPLE
# Check if port 80 is active using process name
IsPortActive -ProcessName "httpd"
.EXAMPLE
# Check if port 8080 is active using process ID
IsPortActive -ProcessId 1234
.NOTES
Version : 1.0.0
Created by : asheroto
#>
#Requires -RunAsAdministrator
[CmdletBinding()]
param (
[Int32]$Port,
[String]$ProcessName,
[Int32]$ProcessId,
[switch]$Version,
[switch]$Help,
[switch]$CheckForUpdate
)
# Version
$CurrentVersion = '1.0.0'
$RepoOwner = 'asheroto'
$RepoName = 'IsPortActive'
# Make sure that -Port, -ProcessName, or -ProcessId are not specified together
if (($Port -and $ProcessName) -or ($Port -and $ProcessId) -or ($ProcessName -and $ProcessId)) {
Write-Error "You can only specify one of the following parameters: -Port, -ProcessName, or -ProcessId"
exit 1
}
# Check if -Version is specified
if ($Version.IsPresent) {
Write-Output "$RepoName $CurrentVersion"
exit 0
}
# Line separator
function LineSeparator {
Return ("─" * 80)
}
# Help
if ($Help) {
Get-Help -Name $MyInvocation.MyCommand.Source -Full
exit 0
}
# Check the GitHub release for the latest version
function Check-GitHubRelease {
param (
[string]$Owner,
[string]$Repo
)
try {
$url = "https://api.github.com/repos/$Owner/$Repo/releases/latest"
$response = Invoke-RestMethod -Uri $url -ErrorAction Stop
$latestVersion = $response.tag_name
$publishedAt = $response.published_at
$UtcDateTimeFormat = "MM/dd/yyyy HH:mm:ss"
# Convert UTC time string to local time
$UtcDateTime = [DateTime]::ParseExact($publishedAt, $UtcDateTimeFormat, $null)
$PublishedLocalDateTime = $UtcDateTime.ToLocalTime()
[PSCustomObject]@{
LatestVersion = $latestVersion
PublishedDateTime = $PublishedLocalDateTime
}
} catch {
Write-Error "Unable to check for updates. Error: $_"
exit 1
}
}
# Check for updates
if ($CheckForUpdate) {
$Data = Check-GitHubRelease -Owner $RepoOwner -Repo $RepoName
if ($Data.LatestVersion -gt $CurrentVersion) {
Write-Warning "$RepoName is out of date.`n"
Write-Output "Current version: $CurrentVersion. Latest version: $($Data.LatestVersion). Published at: $($Data.PublishedDateTime).`n"
Write-Output "You can download the latest version from https://github.com/$RepoOwner/$RepoName/releases`n"
Write-Output "If you have PowerShell 5 or later, you can run"
Write-Output "`tInstall-Script -Name $RepoName -Force"
Write-Output "to install the latest version.`n"
} else {
Write-Output "`n$RepoName is up to date.`n"
Write-Output "Current version: $CurrentVersion. Latest version: $($Data.LatestVersion). Published at: $($Data.PublishedDateTime).`n"
Write-Output "Repository: https://github.com/$RepoOwner/$RepoName/releases`n"
Write-Output "PowerShell Gallery: https://www.powershellgallery.com/packages/$RepoName"
}
exit 0
}
function Get-Connections {
param (
[int]$ProcessId,
[string]$ProcessName,
[int]$Port
)
$tcpConnections = Get-NetTCPConnection
$udpEndpoints = Get-NetUDPEndpoint
if ($ProcessId) {
$tcpConnections = $tcpConnections | Where-Object { $_.OwningProcess -eq $ProcessId }
$udpEndpoints = $udpEndpoints | Where-Object { $_.OwningProcess -eq $ProcessId }
}
if ($ProcessName) {
$processes = Get-Process | Where-Object { $_.Name -eq $ProcessName } -ErrorAction SilentlyContinue
$processIds = $processes.Id
$tcpConnections = $tcpConnections | Where-Object { $processIds -contains $_.OwningProcess }
$udpEndpoints = $udpEndpoints | Where-Object { $processIds -contains $_.OwningProcess }
}
if ($Port) {
$tcpConnections = $tcpConnections | Where-Object { $_.LocalPort -eq $Port }
$udpEndpoints = $udpEndpoints | Where-Object { $_.LocalPort -eq $Port }
}
$Connections = @{}
# Get all services
$services = Get-WmiObject -Query "SELECT * FROM Win32_Service" -ErrorAction SilentlyContinue
# Create a hashtable of service objects
$serviceHashtable = @{}
foreach ($service in $services) {
$serviceHashtable[$service.ProcessID] = $service
}
$tcpConnections | Group-Object -Property LocalAddress | ForEach-Object {
$localAddress = $_.Name
if (-not $Connections.ContainsKey($localAddress)) {
$Connections[$localAddress] = @()
}
$_.Group | ForEach-Object {
$OwningProcess = $_.OwningProcess
$ProcessName = (Get-Process -Id $OwningProcess -ErrorAction SilentlyContinue).Name
$Path = (Get-Process -Id $OwningProcess -ErrorAction SilentlyContinue).Path
$service = $serviceHashtable[$OwningProcess]
if ($ProcessName -eq 'svchost' -and $service) {
$Connection = [PSCustomObject][ordered]@{
"Service Name" = $service.Name
"Display Name" = $service.DisplayName
"Process Name" = $ProcessName
"Process ID" = $OwningProcess
"Path" = $Path
"Local Address" = $_.LocalAddress
"Local Port" = $_.LocalPort
"Remote Address" = $_.RemoteAddress
"Remote Port" = $_.RemotePort
"State" = $_.State
"Protocol" = "TCP"
}
} else {
$Connection = [PSCustomObject][ordered]@{
"Process Name" = $ProcessName
"Process ID" = $OwningProcess
"Path" = $Path
"Local Address" = $_.LocalAddress
"Local Port" = $_.LocalPort
"Remote Address" = $_.RemoteAddress
"Remote Port" = $_.RemotePort
"State" = $_.State
"Protocol" = "TCP"
}
}
$Connections[$localAddress] += $Connection
}
}
$udpEndpoints | Group-Object -Property LocalAddress | ForEach-Object {
$localAddress = $_.Name
if (-not $Connections.ContainsKey($localAddress)) {
$Connections[$localAddress] = @()
}
$_.Group | ForEach-Object {
$OwningProcess = $_.OwningProcess
$ProcessName = (Get-Process -Id $OwningProcess -ErrorAction SilentlyContinue).Name
$Path = (Get-Process -Id $OwningProcess -ErrorAction SilentlyContinue).Path
$service = $serviceHashtable[$OwningProcess]
if ($ProcessName -eq 'svchost' -and $service) {
$Connection = [PSCustomObject][ordered]@{
"Service Name" = $service.Name
"Display Name" = $service.DisplayName
"Process Name" = $ProcessName
"Process ID" = $OwningProcess
"Path" = $Path
"Local Address" = $_.LocalAddress
"Local Port" = $_.LocalPort
"Protocol" = "UDP"
}
} else {
$Connection = [PSCustomObject][ordered]@{
"Process Name" = $ProcessName
"Process ID" = $OwningProcess
"Path" = $Path
"Local Address" = $_.LocalAddress
"Local Port" = $_.LocalPort
"Protocol" = "UDP"
}
}
$Connections[$localAddress] += $Connection
}
}
return $Connections
}
# Update note
Write-Output "$RepoName $CurrentVersion"
Write-Output "To check for updates, run $RepoName -CheckForUpdate`n"
# Validate parameters
if ($Port -gt 0 -and -not [string]::IsNullOrWhiteSpace($ProcessName)) {
Write-Error "You must specify either -Port or -ProcessName, but not both."
Write-Output ""
exit 1
}
# ProcessId is specified, ignore other parameters
if ($ProcessId -gt 0) {
$Connections = Get-Connections -ProcessId $ProcessId
} else {
# Port or ProcessName is specified
if ($Port -gt 0) {
$Connections = Get-Connections -Port $Port
} elseif (-not [string]::IsNullOrWhiteSpace($ProcessName)) {
$Connections = Get-Connections -ProcessName $ProcessName
} else {
Write-Error "You must specify a port number or provide a process name or process ID."
Write-Output ""
exit 1
}
}
# Output connections grouped by local address
$hasActiveConnections = $false
foreach ($localAddress in $Connections.Keys) {
$localConnections = $Connections[$localAddress]
if ($localConnections.Count -gt 0) {
$hasActiveConnections = $true
LineSeparator
Write-Output "Local Address: $localAddress"
LineSeparator
$localConnections | ForEach-Object {
$_ | Write-Output
}
}
}
if (-not $hasActiveConnections) {
Write-Output "No active connections found."
}