1212 scanned Port-Range include Port-Number, Protocol, Service-Name, Service-Description and Status.
1313
1414 . DESCRIPTION
15- This is a powerful asynchronus Port-Scanner working with the PowerShell RunspacePool. You can scan any
16- Port-Range you want. The result will show you all open ports with include Port-Number, Protocol,
17- Service-Name, Service-Description and Status.
15+ This is a powerful asynchronus Port-Scanner working with the PowerShell RunspacePool. You can scan any Port-Range
16+ you want. The result will show you all open ports Port-Number, Protocol, Service-Name, Service-Description and
17+ Status.
1818
1919 This script also work fine along with my asychronus IP-Scanner published on GitHub too. You can easily
2020 pipe the output of the IP-Scanner result in this script.
21+
2122 If you found a bug or have some ideas to improve this script... Let me know. You find my Github profile in
2223 the links below.
2324
2425 . EXAMPLE
25- .\ScanPortsAsync.ps1 -IPv4Address 172.16.0.1 -StartPort 1 -EndPort 1000
26+ .\ScanPortsAsync.ps1 -ComputerName 172.16.0.1 -StartPort 1 -EndPort 1000
2627
2728 . EXAMPLE
28- .\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -UpdateListFromIANA
29+ .\ScanPortsAsync.ps1 -ComputerName 192.168.1.100 -UpdateListFromIANA
2930
3031 . EXAMPLE
31- .\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -Threads 250
32+ .\ScanPortsAsync.ps1 -ComputerName 192.168.1.100 -Threads 250
3233
3334 . LINK
3435 Github Profil: https://github.com/BornToBeRoot
4142 Position = 0 ,
4243 Mandatory = $true ,
4344 HelpMessage = ' Enter IP-Address of the device which you want to scan' )]
44- [IPAddress ] $IPv4Address ,
45+ [String ] $ComputerName ,
4546
4647 [Parameter (
4748 Position = 1 ,
@@ -61,7 +62,13 @@ Param(
6162 [Parameter (
6263 Position = 4 ,
6364 HelpMessage = ' Update Service Name and Transport Protocol Port Number Registry from IANA.org (https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml)' )]
64- [Switch ]$UpdateListFromIANA
65+ [Switch ]$UpdateListFromIANA ,
66+
67+ [Parameter (
68+ Position = 5 ,
69+ HelpMessage = ' Execute script without user interaction'
70+ )]
71+ [Switch ]$Force
6572)
6673
6774Begin {
@@ -124,11 +131,56 @@ Begin{
124131 exit
125132 }
126133
127- if (-not ( Test-Connection - ComputerName $IPv4Address - Count 2 - Quiet))
134+ # Check if Host is reachable
135+ if (-not ( Test-Connection - ComputerName $ComputerName - Count 2 - Quiet))
128136 {
129- Write-Host " IP-Address not reachable!" - ForegroundColor Red
130- exit
137+ Write-Host " $ComputerName not reachable!" - ForegroundColor Red
138+
139+ if ($Force -eq $false )
140+ {
141+ while (" yes" , " no" -notcontains $Answer )
142+ {
143+ $Answer = Read-Host " Would you like to continue? (maybe ICMP is blocked) [yes|no]"
144+ }
145+
146+ if ($Answer -eq " no" )
147+ {
148+ exit
149+ }
150+ }
131151 }
152+
153+ # Check if Hostname or IP-Address
154+ $IPv4Address = [String ]::Empty
155+
156+ if ([bool ]($ComputerName -as [IPAddress ]))
157+ {
158+ $IPv4Address = $ComputerName
159+ }
160+ else
161+ {
162+ # Get IP from Hostname (IPv4 only)
163+ try {
164+ $AddressList = @ (([System.Net.Dns ]::GetHostEntry($ComputerName )).AddressList)
165+
166+ foreach ($Address in $AddressList )
167+ {
168+ if ($Address.AddressFamily -eq " InterNetwork" )
169+ {
170+ $IPv4Address = $Address.IPAddressToString
171+ break
172+ }
173+ }
174+ }
175+ catch { } # Can't get IPAddressList
176+ finally {
177+ if ([String ]::IsNullOrEmpty($IPv4Address ))
178+ {
179+ Write-Host " Could not get IPv4-Address from $ComputerName . (Enter IP-Address instead of Hostname)" - ForegroundColor Red
180+ exit
181+ }
182+ }
183+ }
132184
133185 # Some User-Output about the selected or default settings
134186 Write-Host " `n Script ($ScriptFileName ) started at $StartTime " - ForegroundColor Green
@@ -142,22 +194,22 @@ Begin{
142194Process {
143195 # Scriptblock that will run in runspaces (threads)...
144196 [System.Management.Automation.ScriptBlock ]$ScriptBlock = {
145- # Parameters
146- $IPv4Address = $args [0 ]
147- $Port = $args [1 ]
148-
197+ Param (
198+ $IPv4Address ,
199+ $Port
200+ )
201+
149202 try {
150203 $Socket = New-Object System.Net.Sockets.TcpClient($IPv4Address , $Port )
151204
152205 if ($Socket.Connected )
153206 {
154- $Status = " open "
207+ $Status = " Open "
155208 $Socket.Close ()
156209 }
157210 }
158- catch
159- {
160- $Status = " closed"
211+ catch {
212+ $Status = " Closed"
161213 }
162214
163215 $Result = New-Object - TypeName PSObject
@@ -184,12 +236,25 @@ Process{
184236
185237 foreach ($Port in $StartPort .. $EndPort )
186238 {
187- if ($PortRange -gt 0 ) { $Progress_Percent = (($Port - $StartPort ) / $PortRange ) * 100 } else { $Progress_Percent = 100 }
188- Write-Progress - Activity " Setting up jobs..." - Id 1 - Status " Current Port: $Port " - PercentComplete ($Progress_Percent )
239+ $ScriptParams = @ {
240+ IPv4Address = $IPv4Address
241+ Port = $Port
242+ }
243+
244+ if ($PortRange -gt 0 )
245+ {
246+ $Progress_Percent = (($Port - $StartPort ) / $PortRange ) * 100
247+ }
248+ else
249+ {
250+ $Progress_Percent = 100
251+ }
252+
253+ Write-Progress - Activity " Setting up jobs..." - Id 1 - Status " Current Port: $Port " - PercentComplete ($Progress_Percent )
189254
190- $Job = [System.Management.Automation.PowerShell ]::Create().AddScript($ScriptBlock ).AddArgument( $IPv4Address ).AddArgument( $Port )
255+ $Job = [System.Management.Automation.PowerShell ]::Create().AddScript($ScriptBlock ).AddParameters( $ScriptParams )
191256 $Job.RunspacePool = $RunspacePool
192- $Jobs += New-Object psobject - Property @ {
257+ $Jobs += New-Object PSObject - Property @ {
193258 RunNum = $Port - $StartPort
194259 Pipe = $Job
195260 Result = $Job.BeginInvoke ()
@@ -223,13 +288,13 @@ Process{
223288
224289 $RunspacePool.Close ()
225290
226- # Only get open ports (others are closed -.- )
227- $Ports_Open = $Jobs_Result | Where-Object {$_.Status -eq " open " }
291+ # Only get open ports (others are closed :/ )
292+ $Ports_Open = $Jobs_Result | Where-Object {$_.Status -eq " Open " }
228293
229294 Write-Host " [" - ForegroundColor Gray - NoNewline; Write-Host " Done" - ForegroundColor Green - NoNewline; Write-Host " ]" - ForegroundColor Gray
230295
231296 # Assign service with ports
232- if ($ AssignServiceWithPorts )
297+ if (( $Ports_Open -ne $null ) -and ( $ AssignServiceWithPorts) )
233298 {
234299 Write-Host " Assign services to ports...`t`t " - ForegroundColor Yellow - NoNewline
235300
@@ -268,13 +333,13 @@ Process{
268333
269334End {
270335 # If no XML-File to assign service with port... only show open ports
271- if ($AssignServiceWithPorts )
336+ if (( $Ports_Open -ne $null ) -and ( $AssignServiceWithPorts ))
272337 {
273338 $Results = $Ports_Open_Assigned
274339 }
275340 else
276341 {
277- $Results = $Ports_Open
342+ $Results = $Ports_Open
278343 }
279344
280345 # Time when the Script finished
294359
295360 # Return custom psobject with Port status
296361 return $Results
297- }
362+ }
0 commit comments