Created
October 25, 2017 16:10
-
-
Save JohnLBevan/6ab9d7fa45459a3bdc4ea5e9190ea556 to your computer and use it in GitHub Desktop.
Scan a given range of ports for a specific server.
NB: Optimum group size is ~250
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cls | |
| workflow Test-PortOpenQuickly { | |
| [cmdletbinding()] | |
| param( | |
| [Parameter(Mandatory = $true)] | |
| [string]$ComputerName | |
| , | |
| [Parameter(Mandatory = $true)] | |
| [int[]]$Ports | |
| , | |
| [Parameter(Mandatory = $false)] | |
| [int$GroupSize = 250 | |
| ) | |
| $portGroups = InlineScript { | |
| [psobject]$itemCounter = [pscustomobject]@{itemNo=0;groupSize=$using:GroupSize} | |
| $itemCounter | Add-Member -MemberType ScriptMethod -Name 'groupNo' -Value { | |
| [math]::Floor($this.itemNo++ / $this.groupSize) | |
| } | |
| write-output $using:Ports | Group-Object -Property {$itemCounter.groupNo()} | |
| } | |
| ForEach -parallel ($portGroup in $portGroups) { | |
| $portGroup | select -ExpandProperty group | %{ | |
| try { | |
| $socket = new-object System.Net.Sockets.TcpClient -ArgumentList @('ccoportal', $_) -ErrorAction SilentlyContinue | |
| If($socket.Connected){ | |
| $Open = $true | |
| $socket.Close() | |
| } | |
| } catch { | |
| $Open = $false | |
| } | |
| write-output (new-object -type psobject -property @{ComputerName=$ComputerName;Port=$_;Open=$Open}) | |
| } | |
| } | |
| } | |
| [System.Diagnostics.Stopwatch]$stopwatch = [System.Diagnostics.Stopwatch]::StartNew() | |
| Test-PortOpenQuickly -ComputerName 'myComputer' -Ports (0..65535) | ? Open | sort Port | ft ComputerName, Port, Open | |
| $stopwatch.Stop() | |
| $stopwatch.Elapsed | |
| #group size 10 => ~1 hour 13 mins | |
| #group size 100 => 30 mins | |
| #group size 250 => 26 mins | |
| #group size 1000 => 35 mins |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment