Test-TCPPort.psm1
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 |
<#
.Synopsis Opens a TCP Port on the Local Host for 30-35 Seconds. .Description Opens a TCP Port on the Local Host for 30-35 Seconds. .Parameter Port Port Number you want to open. .Example # Opens a TCP Port on the Local Host for 30-35 Seconds. Test-TCPPort -Port 5555 .Example # Opens a TCP Port on the Local Host for 30-35 Seconds, with netstat output. Test-TCPPort -Port 5555 -Details #> #------------------------------------------------------------------------------ # # # THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT # WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT # LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS # FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR # RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER. # #------------------------------------------------------------------------------ function Test-TCPPort { Param ( [Parameter(Mandatory=$true)] [int]$Port, [Switch]$Details ) #$port = 5555 # Creating TCP Listener $listener = New-Object System.Net.Sockets.TcpListener($Port) if($port -eq 0){Write-Host "Enter Port number between: 1-65535" -ForegroundColor Yellow; Break} if($port -gt 65535){Write-Host "Enter Port number between: 1-65535" -ForegroundColor Yellow; Break} Write-Host "TCP Port $($port) will be in listening for 30-35 Seconds; to close the TCP Port manually close the PowerShell window" -ForegroundColor Green $listener.Start() # Putting a 30 Second Delay For($i=1;$i -lt 30; $i++){ Start-Sleep -Seconds 1 $x = Get-NetTCPConnection -LocalPort $port } # Putting a 20 Second Delay #Start-Sleep -Seconds 30 $listener.Stop() if($Details){$x} Else{ } } Export-ModuleMember -Function Test-TCPPort #Test-TCPPort -Port 5555 #Test-TCPPort -Port 5555 -Details |