Skip to content

Commit c7c4175

Browse files
committed
Code improved
1 parent ea6e927 commit c7c4175

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

Scripts/IPv4PortScan.ps1

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ param(
4545
Position=2,
4646
HelpMessage='Last port which should be scanned (Default=65535)')]
4747
[ValidateRange(1,65535)]
48+
[ValidateScript({
49+
if($_ -lt $StartPort)
50+
{
51+
throw "Invalid Port-Range!"
52+
}
53+
else
54+
{
55+
return $true
56+
}
57+
})]
4858
[Int32]$EndPort=65535,
4959

5060
[Parameter(
@@ -79,8 +89,8 @@ Begin{
7989
try{
8090
Write-Verbose -Message "Create backup of the IANA Service Name and Transport Protocol Port Number Registry..."
8191

82-
# Backup file, before donload a new version
83-
if([System.IO.File]::Exists($XML_PortList_Path))
92+
# Backup file, before download a new version
93+
if(Test-Path -Path $XML_PortList_Path -PathType Leaf)
8494
{
8595
Rename-Item -Path $XML_PortList_Path -NewName $XML_PortList_BackupPath
8696
}
@@ -93,7 +103,7 @@ Begin{
93103
$New_XML_PortList.Save($XML_PortList_Path)
94104

95105
# Remove backup, if no error
96-
if([System.IO.File]::Exists($XML_PortList_BackupPath))
106+
if(Test-Path -Path $XML_PortList_BackupPath -PathType Leaf)
97107
{
98108
Remove-Item -Path $XML_PortList_BackupPath
99109
}
@@ -102,12 +112,12 @@ Begin{
102112
Write-Verbose -Message "Cleanup downloaded file and restore backup..."
103113

104114
# On error: cleanup downloaded file and restore backup
105-
if([System.IO.File]::Exists($XML_PortList_Path))
115+
if(Test-Path -Path $XML_PortList_Path -PathType Leaf)
106116
{
107117
Remove-Item -Path $XML_PortList_Path -Force
108118
}
109119

110-
if([System.IO.File]::Exists($XML_PortList_BackupPath))
120+
if(Test-Path -Path $XML_PortList_BackupPath -PathType Leaf)
111121
{
112122
Rename-Item -Path $XML_PortList_BackupPath -NewName $XML_PortList_Path
113123
}
@@ -157,17 +167,19 @@ Begin{
157167
}
158168

159169
Process{
170+
$Xml_PortList_Available = Test-Path -Path $XML_PortList_Path -PathType Leaf
171+
160172
if($UpdateList)
161173
{
162174
UpdateListFromIANA
163175
}
164-
elseif(-Not([System.IO.File]::Exists($XML_PortList_Path)))
176+
elseif($Xml_PortList_Available -eq $false)
165177
{
166178
Write-Warning -Message "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."
167179
}
168180

169181
# Check if it is possible to assign service with port --> import xml-file
170-
if([System.IO.File]::Exists($XML_PortList_Path))
182+
if($Xml_PortList_Available)
171183
{
172184
$AssignServiceWithPort = $true
173185

@@ -178,12 +190,6 @@ Process{
178190
$AssignServiceWithPort = $false
179191
}
180192

181-
# Validate Port-Range
182-
if($StartPort -gt $EndPort)
183-
{
184-
throw "Invalid Port-Range... Check your input!"
185-
}
186-
187193
# Check if host is reachable
188194
Write-Verbose -Message "Test if host is reachable..."
189195
if(-not(Test-Connection -ComputerName $ComputerName -Count 2 -Quiet))

0 commit comments

Comments
 (0)