Skip to content

Commit 684df06

Browse files
committed
Bugfix / Code Improved
1 parent d473bda commit 684df06

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Scripts/ScanPortsAsync.ps1

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,8 @@ Begin{
7777

7878
# Local path to PortList
7979
$XML_PortList_Path = "$Script_Startup_Path\ServiceName_and_TransportProtocolPortNumber_Registry.xml"
80-
81-
# Validate Port-Range
82-
if($StartPort -gt $EndPort)
83-
{
84-
Write-Host "Check your input! Invalid Port-Range... (-StartPort can't be lower than -EndPort)" -ForegroundColor Red
85-
exit
86-
}
8780

88-
$PortRange = ($EndPort - $StartPort)
89-
90-
# Port list can be updated from IANA.org with the parameter "-UpdateListFromIANA
81+
# Port list can be updated from IANA.org with the parameter "-UpdateListFromIANA
9182
if($UpdateListFromIANA)
9283
{
9384
try
@@ -96,7 +87,10 @@ Begin{
9687

9788
[xml]$New_XML_PortList = Invoke-WebRequest -Uri $IANA_PortList_WebUri # Download latest xml-file from IANA
9889

99-
Remove-Item $XML_PortList_Path -Force -ErrorAction SilentlyContinue # Don`t show errors if there is no old file
90+
if([System.IO.File]::Exists($XML_PortList_Path))
91+
{
92+
Remove-Item -Path $XML_PortList_Path
93+
}
10094

10195
$New_XML_PortList.Save($XML_PortList_Path) # Save xml-file
10296

@@ -106,7 +100,7 @@ Begin{
106100
{
107101
$ErrorMsg = $_.Exception.Message
108102

109-
Write-Host "Update Service Name and Transport Protocol Port Number Registry from IANA failed with the follwing error message: $ErrorMsg" -ForegroundColor Red
103+
Write-Host "Update Service Name and Transport Protocol Port Number Registry from IANA.org failed with the follwing error message: $ErrorMsg" -ForegroundColor Red
110104
}
111105
}
112106
elseif(-Not([System.IO.File]::Exists($XML_PortList_Path)))
@@ -122,6 +116,13 @@ Begin{
122116
{
123117
$AssignServiceWithPorts = $false
124118
}
119+
120+
# Validate Port-Range
121+
if($StartPort -gt $EndPort)
122+
{
123+
Write-Host "Check your input! Invalid Port-Range... (-StartPort can't be lower than -EndPort)" -ForegroundColor Red
124+
exit
125+
}
125126

126127
if(-not( Test-Connection -ComputerName $IPv4Address -Count 2 -Quiet))
127128
{
@@ -179,6 +180,8 @@ Process{
179180
#Setting up jobs
180181
Write-Host "Setting up jobs...`t`t`t" -ForegroundColor Yellow -NoNewline
181182

183+
$PortRange = ($EndPort - $StartPort)
184+
182185
foreach($Port in $StartPort..$EndPort)
183186
{
184187
if($PortRange -gt 0) { $Progress_Percent = (($Port - $StartPort) / $PortRange) * 100 } else { $Progress_Percent = 100 }
@@ -271,12 +274,14 @@ End{
271274
$Results = $Ports_Open
272275
}
273276

277+
# Time when the Script finished
274278
$EndTime = Get-Date
275279

280+
# Calculate the time between Start and End
276281
$ExecutionTimeMinutes = (New-TimeSpan -Start $StartTime -End $EndTime).Minutes
277282
$ExecutionTimeSeconds = (New-TimeSpan -Start $StartTime -End $EndTime).Seconds
278283

279-
# Some User-Output with Device UP/Down and execution time
284+
# Some User-Output with ports scanned/up and execution time
280285
Write-Host "`n+=-=-=-=-=-=-=-=-=-=-=-= Result =-=-=-=-=-=-=-=-=-=-=-=`n|"
281286
Write-Host "| Ports Scanned:`t$($Jobs_Result.Count)"
282287
Write-Host "| Ports Open:`t`t$(@($Results | Where-Object {($_.Status -eq "open")}).Count)"
@@ -286,4 +291,4 @@ End{
286291

287292
# Return custom psobject with Port status
288293
return $Results
289-
}
294+
}

0 commit comments

Comments
 (0)