mirror of
https://github.com/Xevion/powershell.git
synced 2025-12-05 23:15:46 -06:00
CheckExcludedPorts PowerShell script
This commit is contained in:
36
content/scripts/CheckExcludedPorts.ps1
Normal file
36
content/scripts/CheckExcludedPorts.ps1
Normal file
@@ -0,0 +1,36 @@
|
||||
<#
|
||||
.Description
|
||||
This script checks if the given port has been placed in a excluded port range.
|
||||
This script utilizes the netsh command and only observes TCP exclusion ranges.
|
||||
#>
|
||||
param (
|
||||
[Parameter(Mandatory=$true)]
|
||||
[Int32]
|
||||
$targetPort
|
||||
)
|
||||
|
||||
# Filters excluded port range output to just the ports
|
||||
$rawPortRanges = netsh interface ipv4 show excludedportrange protocol=tcp | Select-String "\d+\s+\d+"
|
||||
# Cleans the output to include just start/end port with a single space
|
||||
$cleanPortRanges = $rawPortRanges -replace "\s+(\d+)\s+(\d+).*", '$1 $2'
|
||||
|
||||
$found = $false
|
||||
$cleanPortRanges | % {
|
||||
# Break apart the port, intrepret as int
|
||||
$start, $end = $_.toString().split(' ')
|
||||
$start = $start -as [int]
|
||||
$end = $end -as [int]
|
||||
|
||||
# Complete the check
|
||||
if ($targetPort -In $start..$end) {
|
||||
$found = $true
|
||||
Write-Host "${start} - ${end}"
|
||||
Write-Host "`tPort ${targetPort} is in an excluded port range."
|
||||
Break # Assumption: Excluded port ranges do not overlap
|
||||
}
|
||||
}
|
||||
|
||||
# Default output
|
||||
if (!$found) {
|
||||
Write-Host "Port ${targetPort} is not in an excluded port range."
|
||||
}
|
||||
13
content/scripts/excluded-ports.md
Normal file
13
content/scripts/excluded-ports.md
Normal file
@@ -0,0 +1,13 @@
|
||||
+++
|
||||
title = "Check Excluded Port Ranges"
|
||||
summary = "Check if a specific port is in an excluded port range with this simple script."
|
||||
date = 2022-08-26
|
||||
+++
|
||||
|
||||
This script was built during an internship (and properly requested) to check excluded port ranges.
|
||||
|
||||
Raw file available at {{% absolute_url "./scripts/CheckExcludedPorts.ps1" %}}
|
||||
|
||||
```powershell
|
||||
{{% file "/scripts/CheckExcludedPorts.ps1" %}}
|
||||
```
|
||||
Reference in New Issue
Block a user