Skip to content

Instantly share code, notes, and snippets.

@soulemike
Created October 24, 2022 14:32
Show Gist options
  • Select an option

  • Save soulemike/747416677c4e50b80e6d4c376e7f6db1 to your computer and use it in GitHub Desktop.

Select an option

Save soulemike/747416677c4e50b80e6d4c376e7f6db1 to your computer and use it in GitHub Desktop.
Concurrent Test-NetConnection Port Scan
$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