Skip to content

Commit 1a38a35

Browse files
committed
Code improved / better error handling
1 parent e99fd8a commit 1a38a35

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

Scripts/New-IPv4NetworkScan.ps1

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Param(
123123
)
124124

125125
Begin{
126-
Write-Verbose "Script started at $(Get-Date)"
126+
Write-Verbose -Message "Script started at $(Get-Date)"
127127

128128
# IEEE -> The Public Listing For IEEE Standards Registration Authority -> CSV-File
129129
$IEEE_MACVendorList_WebUri = "http://standards.ieee.org/develop/regauth/oui/oui.csv"
@@ -137,20 +137,20 @@ Begin{
137137
{
138138
# Try to download the MAC-Vendor list from IEEE
139139
try{
140-
Write-Verbose "Create backup of the IEEE Standards Registration Authority list..."
140+
Write-Verbose -Message "Create backup of the IEEE Standards Registration Authority list..."
141141

142142
# Backup file, before download a new version
143143
if([System.IO.File]::Exists($CSV_MACVendorList_Path))
144144
{
145145
Rename-Item -Path $CSV_MACVendorList_Path -NewName $CSV_MACVendorList_BackupPath
146146
}
147147

148-
Write-Verbose "Updating IEEE Standards Registration Authority from IEEE.org..."
148+
Write-Verbose -Message "Updating IEEE Standards Registration Authority from IEEE.org..."
149149

150150
# Download csv-file from IEEE
151151
Invoke-WebRequest -Uri $IEEE_MACVendorList_WebUri -OutFile $CSV_MACVendorList_Path -ErrorAction Stop
152152

153-
Write-Verbose "Remove backup of the IEEE Standards Registration Authority list..."
153+
Write-Verbose -Message "Remove backup of the IEEE Standards Registration Authority list..."
154154

155155
# Remove Backup, if no error
156156
if([System.IO.File]::Exists($CSV_MACVendorList_BackupPath))
@@ -159,7 +159,7 @@ Begin{
159159
}
160160
}
161161
catch{
162-
Write-Verbose "Cleanup downloaded file and restore backup..."
162+
Write-Verbose -Message "Cleanup downloaded file and restore backup..."
163163

164164
# On error: cleanup downloaded file and restore backup
165165
if([System.IO.File]::Exists($CSV_MACVendorList_Path))
@@ -224,12 +224,10 @@ Begin{
224224
}
225225
}
226226

227-
$Result = [pscustomobject] @{
227+
[pscustomobject] @{
228228
Mask = $Mask
229229
CIDR = $CIDR
230230
}
231-
232-
return $Result
233231
}
234232

235233
End {
@@ -277,12 +275,10 @@ Begin{
277275
}
278276
}
279277

280-
$Result = [pscustomobject] @{
278+
[pscustomobject] @{
281279
IPv4Address = $IPv4Address
282280
Int64 = $Int64
283281
}
284-
285-
return $Result
286282
}
287283

288284
End {
@@ -362,14 +358,12 @@ Begin{
362358
$Hosts = ($AvailableIPs - 2)
363359

364360
# Build custom PSObject
365-
$Result = [pscustomobject] @{
361+
[pscustomobject] @{
366362
NetworkID = $NetworkID
367363
Broadcast = $Broadcast
368364
IPs = $AvailableIPs
369365
Hosts = $Hosts
370366
}
371-
372-
return $Result
373367
}
374368

375369
End{
@@ -407,7 +401,7 @@ Begin{
407401
}
408402
}
409403

410-
$NewResult = [pscustomobject] @{
404+
[pscustomobject] @{
411405
IPv4Address = $Result.IPv4Address
412406
Status = $Result.Status
413407
Hostname = $Result.Hostname
@@ -417,8 +411,6 @@ Begin{
417411
ResponseTime = $Result.ResponseTime
418412
TTL = $Result.TTL
419413
}
420-
421-
return $NewResult
422414
}
423415

424416
End {
@@ -435,7 +427,7 @@ Process{
435427
}
436428
elseif(($EnableMACResolving) -and (-Not([System.IO.File]::Exists($CSV_MACVendorList_Path))))
437429
{
438-
Write-Host 'No CSV-File to assign vendor with MAC-Address found! Use the parameter "-UpdateList" to download the latest version from IEEE.org. This warning doesn`t affect the scanning procedure.' -ForegroundColor Yellow
430+
Write-Warning -Message "No CSV-File to assign vendor with MAC-Address found! Use the parameter ""-UpdateList"" to download the latest version from IEEE.org. This warning doesn`t affect the scanning procedure."
439431
}
440432

441433
# Calculate Subnet (Start and End IPv4-Address)
@@ -462,16 +454,15 @@ Process{
462454
# Check if range is valid
463455
if($StartIPv4Address_Int64 -gt $EndIPv4Address_Int64)
464456
{
465-
Write-Host "Invalid IP-Range... Check your input!" -ForegroundColor Red
466-
return
457+
Write-Error -Message "Invalid IP-Range... Check your input!" -Category InvalidArgument -ErrorAction Stop
467458
}
468459

469460
# Calculate IPs to scan (range)
470461
$IPsToScan = ($EndIPv4Address_Int64 - $StartIPv4Address_Int64)
471462

472-
Write-Verbose "Scanning range from $StartIPv4Address to $EndIPv4Address ($($IPsToScan + 1) IPs)"
473-
Write-Verbose "Running with max $Threads threads"
474-
Write-Verbose "ICMP checks per IP is set to $Tries"
463+
Write-Verbose -Message "Scanning range from $StartIPv4Address to $EndIPv4Address ($($IPsToScan + 1) IPs)"
464+
Write-Verbose -Message "Running with max $Threads threads"
465+
Write-Verbose -Message "ICMP checks per IP is set to $Tries"
475466

476467
# Properties which are displayed in the output
477468
$PropertiesToDisplay = @()
@@ -621,14 +612,14 @@ Process{
621612
}
622613
}
623614

624-
Write-Verbose "Setting up RunspacePool..."
615+
Write-Verbose -Message "Setting up RunspacePool..."
625616

626617
# Create RunspacePool and Jobs
627618
$RunspacePool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, $Threads, $Host)
628619
$RunspacePool.Open()
629620
[System.Collections.ArrayList]$Jobs = @()
630621

631-
Write-Verbose "Setting up Jobs..."
622+
Write-Verbose -Message "Setting up Jobs..."
632623

633624
# Set up Jobs for each IP...
634625
for ($i = $StartIPv4Address_Int64; $i -le $EndIPv4Address_Int64; $i++)
@@ -702,7 +693,7 @@ Process{
702693

703694
Write-Progress -Activity "Waiting for jobs to complete... ($($Threads - $($RunspacePool.GetAvailableRunspaces())) of $Threads threads running)" -Id 1 -PercentComplete $Progress_Percent -Status "$Jobs_Remaining remaining..."
704695

705-
Write-Verbose "Processing $(if($Jobs_ToProcess.Count -eq $null){"1"}else{$Jobs_ToProcess.Count}) job(s)..."
696+
Write-Verbose -Message "Processing $(if($Jobs_ToProcess.Count -eq $null){"1"}else{$Jobs_ToProcess.Count}) job(s)..."
706697

707698
# Processing completed jobs
708699
foreach($Job in $Jobs_ToProcess)
@@ -730,13 +721,13 @@ Process{
730721

731722
} While ($Jobs.Count -gt 0)
732723

733-
Write-Verbose "Closing RunspacePool and free resources..."
724+
Write-Verbose -Message "Closing RunspacePool and free resources..."
734725

735726
# Close the RunspacePool and free resources
736727
$RunspacePool.Close()
737728
$RunspacePool.Dispose()
738729

739-
Write-Verbose "Script finished at $(Get-Date)"
730+
Write-Verbose -Message "Script finished at $(Get-Date)"
740731
}
741732

742733
End{

0 commit comments

Comments
 (0)