Create Start-Portscan.ps1

This commit is contained in:
David Morton
2022-07-21 10:05:36 -05:00
parent 72fc92943b
commit c75e3c2928

37
Start-Portscan.ps1 Normal file
View File

@@ -0,0 +1,37 @@
function Start-Portscan{
[CmdletBinding()]
param (
#Variable for network address
[Parameter(Mandatory=$true,ValueFromPipeline)]
[String[]]
$IPAddress,
#Variable for Port value if defined; otherwise range 1..65535
[Parameter(Mandatory=$false, ValueFromPipeline)]
[String[]]
$Ports
)
$range=1..254
$ErrorActionPreference='SilentyContinue'
if($null -eq $Ports){
$Ports=1..65535
}
$NetworkAddress=($IPAddress.Split(".")[0..2]) -join(".")
foreach($add in $range){
$ip="{0}.{1}" -f $NetworkAddress,$add
if(Test-Connection -Count 1 -ComputerName $ip){
$Ports | ForEach-Object -ThrottleLimit 5 -Parallel{
Write-Progress "Scanning Network" $ip -PercentComplete (($_/$Ports.count)*100)
$Socket = New-Object System.Net.Sockets.TcpClient($ip, $_)
if($Socket.Connected){
Write-Output "$ip port $_ is open."
$Socket.Close()
}
else{
Write-Output "$ip port $_ is not open."
}
}
}
}
}