1+ # ##############################################################################################################
2+ # Language : PowerShell 4.0
3+ # Filename : New-IPv4PortScan.ps1
4+ # Autor : BornToBeRoot (https://github.com/BornToBeRoot)
5+ # Description : Powerful asynchronus IPv4 Port Scanner
6+ # Repository : https://github.com/BornToBeRoot/PowerShell_IPv4PortScanner
7+ # ##############################################################################################################
8+
9+ <#
10+ . SYNOPSIS
11+ Powerful asynchronus IPv4 Port Scanner
12+
13+ . DESCRIPTION
14+
15+ . EXAMPLE
16+
17+ . EXAMPLE
18+
19+ . LINK
20+ https://github.com/BornToBeRoot/PowerShell_IPv4PortScanner
21+ #>
22+
23+ [CmdletBinding ()]
24+ param (
25+ [Parameter (
26+ Position = 0 ,
27+ Mandatory = $true ,
28+ HelpMessage = ' ComputerName or IPv4-Address of the device which you want to scan' )]
29+ [String ]$ComputerName ,
30+
31+ [Parameter (
32+ Position = 1 ,
33+ HelpMessage = ' First port which should be scanned (Default=1)' )]
34+ [Int32 ]$StartPort = 1 ,
35+
36+ [Parameter (
37+ Position = 2 ,
38+ HelpMessage = ' Last port which should be scanned (Default=65535)' )]
39+ [Int32 ]$EndPort = 65535 ,
40+
41+ [Parameter (
42+ Position = 3 ,
43+ HelpMessage = ' Maximum number of threads at the same time (Default=100)' )]
44+ [Int32 ]$Threads = 100 ,
45+
46+ [Parameter (
47+ Position = 4 ,
48+ HelpMessage = ' Execute script without user interaction' )]
49+ [switch ]$Force ,
50+
51+ [Parameter (
52+ Position = 5 ,
53+ HelpMessage = ' Update Service Name and Transport Protocol Port Number Registry from IANA.org' )]
54+ [switch ]$UpdateList
55+ )
56+
57+ Begin {
58+ Write-Verbose " Script started at $ ( Get-Date ) "
59+
60+ # IANA --> Service Name and Transport Protocol Port Number Registry -> xml-file
61+ $IANA_PortList_WebUri = " https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml"
62+
63+ # Port list path
64+ $XML_PortList_Path = " $PSScriptRoot \IANA_ServiceName_and_TransportProtocolPortNumber_Registry.xml"
65+ $XML_PortList_BackupPath = " $PSScriptRoot \IANA_ServiceName_and_TransportProtocolPortNumber_Registry.xml.bak"
66+
67+ # Function to update the list from IANA (Port list)
68+ function UpdateListFromIANA
69+ {
70+ try {
71+ Write-Verbose " Create backup of the IANA Service Name and Transport Protocol Port Number Registry..."
72+
73+ # Backup file, before donload a new version
74+ if ([System.IO.File ]::Exists($XML_PortList_Path ))
75+ {
76+ Rename-Item - Path $XML_PortList_Path - NewName $XML_PortList_BackupPath
77+ }
78+
79+ Write-Verbose " Updating Service Name and Transport Protocol Port Number Registry from IANA.org..."
80+
81+ # Download xml-file from IANA and save it
82+ [xml ]$New_XML_PortList = Invoke-WebRequest - Uri $IANA_PortList_WebUri - ErrorAction Stop
83+
84+ $New_XML_PortList.Save ($XML_PortList_Path )
85+
86+ # Remove backup, if no error
87+ if ([System.IO.File ]::Exists($XML_PortList_BackupPath ))
88+ {
89+ Remove-Item - Path $XML_PortList_BackupPath
90+ }
91+ }
92+ catch {
93+ Write-Verbose " Cleanup downloaded file and restore backup..."
94+
95+ # On error: cleanup downloaded file and restore backup
96+ if ([System.IO.File ]::Exists($XML_PortList_Path ))
97+ {
98+ Remove-Item - Path $XML_PortList_Path - Force
99+ }
100+
101+ if ([System.IO.File ]::Exists($XML_PortList_BackupPath ))
102+ {
103+ Rename-Item - Path $XML_PortList_BackupPath - NewName $XML_PortList_Path
104+ }
105+ }
106+ }
107+ }
108+
109+ Process {
110+ if ($UpdateList.IsPresent )
111+ {
112+ UpdateListFromIANA
113+ }
114+ elseif (-Not ([System.IO.File ]::Exists($XML_PortList_Path )))
115+ {
116+ Write-Host ' No xml-file to assign service with port found! Use the parameter "-UpdateList" to download the latest version from IANA.org. This warning doesn`t affect the scanning procedure.'
117+ }
118+
119+ if ([System.IO.File ]::Exists($XML_PortList_Path ))
120+ {
121+ $AssignServiceWithPort = $true
122+ }
123+ else
124+ {
125+ $AssignServiceWithPort = $false
126+ }
127+ }
128+
129+ End {
130+
131+ }
0 commit comments