Created
October 24, 2022 14:32
-
-
Save soulemike/747416677c4e50b80e6d4c376e7f6db1 to your computer and use it in GitHub Desktop.
Concurrent Test-NetConnection Port Scan
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
| $remoteDomain=@( | |
| "10.0.0.10", | |
| "10.0.0.20", | |
| "domain.local" | |
| ) | |
| function New-Task([int]$index,[string]$target,[int]$port,[scriptblock]$ScriptBlock) | |
| { | |
| $ps = [Management.Automation.PowerShell]::Create() | |
| $res = New-Object PSObject -Property @{ | |
| Index = $Index | |
| Target = $target | |
| Port = $port | |
| Busy = $true | |
| Success = $null | |
| async = $null | |
| } | |
| [Void] $ps.AddScript($ScriptBlock) | |
| [Void] $ps.AddParameter("TaskInfo",$Res) | |
| $res.async = $ps.BeginInvoke() | |
| $res | |
| } | |
| $ScriptBlock = { | |
| param([Object]$TaskInfo) | |
| $TaskInfo.Success = (Test-NetConnection $TaskInfo.Target -Port $TaskInfo.Port).TcpTestSucceeded | |
| $TaskInfo.Busy = $false | |
| } | |
| $connectivityResults=@() | |
| foreach($r in $remoteDomain) | |
| { | |
| $i=0 | |
| $ports=80,443,53,88,135,389,445,636,5985,9389,3268 | |
| $results=@() | |
| foreach($port in $ports) | |
| { | |
| if(($i+1)%50 -eq 0){Start-Sleep -Seconds 5} | |
| $results+=New-Task -index $i -target $r -port $port -ScriptBlock $ScriptBlock | |
| $i++ | |
| } | |
| $connectivityResults+=$results | |
| } | |
| while(($connectivityResults|?{$_.Busy}|measure).Count -gt 0){"Not Done";Start-Sleep -Seconds 1} | |
| $connectivityResults|select index,target,port,data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment