-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGet-ListeningPort.ps1
31 lines (30 loc) · 1.17 KB
/
Get-ListeningPort.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
<#
.Synopsis
Enumerate local listening ports.
.DESCRIPTION
Enumerate all of the local listening ports on a computer (IPv4 only). Requires command Get-NetTCPConnection.
.PARAMETER Ports
The Ports parameter is defaulted to a list of popular server ports.
.EXAMPLE
Get-ListeningPort
LocalAddress LocalPort RemoteAddress RemotePort State AppliedSetting OwningProcess
------------ --------- ------------- ---------- ----- -------------- -------------
0.0.0.0 3389 0.0.0.0 0 Listen 1116
.EXAMPLE
Get-ListeningPort -Ports 80,443
.NOTES
Created by: Jason Wasser
Modified: 1/23/2020
#>
function Get-ListeningPort {
param (
$Ports = @(22,25,443,465,587,636,993,995,3389)
)
$IPv4Regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
$ListeningPorts = Get-NetTCPConnection -State Listen | Where-Object -FilterScript {$_.LocalAddress -match $IPv4Regex}
foreach ($Port in $ListeningPorts) {
if ($Ports -contains $Port.LocalPort) {
Write-Output $Port
}
}
}