Skip to content

Commit edc8e78

Browse files
committed
Performance improve / Progress percent fixed
1 parent f0290f5 commit edc8e78

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

Scripts/New-IPv4NetworkScan.ps1

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ Begin{
176176
}
177177
}
178178

179+
# Check for Update
180+
if($UpdateList.IsPresent)
181+
{
182+
UpdateListFromIEEE
183+
}
184+
elseif(($EnableMACResolving.IsPresent) -and (-Not([System.IO.File]::Exists($CSV_MACVendorList_Path))))
185+
{
186+
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
187+
}
188+
179189
# Helper function to convert a subnetmask
180190
function Convert-Subnetmask
181191
{
@@ -413,29 +423,6 @@ Begin{
413423
}
414424

415425
Process{
416-
# Check for Update
417-
if($UpdateList.IsPresent)
418-
{
419-
UpdateListFromIEEE
420-
}
421-
elseif(($EnableMACResolving.IsPresent) -and (-Not([System.IO.File]::Exists($CSV_MACVendorList_Path))))
422-
{
423-
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
424-
}
425-
426-
# Check if it is possible to assign vendor to MAC and import CSV-File
427-
if(($EnableMACResolving.IsPresent) -and ([System.IO.File]::Exists($CSV_MACVendorList_Path)))
428-
{
429-
$AssignVendorToMAC = $true
430-
431-
# Import the CSV-File
432-
$MAC_VendorList = Import-Csv -Path $CSV_MACVendorList_Path | Select-Object "Assignment", "Organization Name"
433-
}
434-
else
435-
{
436-
$AssignVendorToMAC = $false
437-
}
438-
439426
# Calculate Subnet (Start and End IPv4-Address)
440427
if($PSCmdlet.ParameterSetName -eq 'CIDR' -or $PSCmdlet.ParameterSetName -eq 'Mask')
441428
{
@@ -470,6 +457,19 @@ Process{
470457
Write-Verbose "Scanning range from $StartIPv4Address to $EndIPv4Address ($($IPsToScan + 1) IPs)"
471458
Write-Verbose "Running with max $Threads threads"
472459
Write-Verbose "ICMP checks per IP is set to $Tries"
460+
461+
# Check if it is possible to assign vendor to MAC and import CSV-File
462+
if(($EnableMACResolving.IsPresent) -and ([System.IO.File]::Exists($CSV_MACVendorList_Path)))
463+
{
464+
$AssignVendorToMAC = $true
465+
466+
# Import the CSV-File
467+
$MAC_VendorList = Import-Csv -Path $CSV_MACVendorList_Path | Select-Object "Assignment", "Organization Name"
468+
}
469+
else
470+
{
471+
$AssignVendorToMAC = $false
472+
}
473473

474474
# Scriptblock --> will run in runspaces (threads)...
475475
[System.Management.Automation.ScriptBlock]$ScriptBlock = {
@@ -626,25 +626,25 @@ Process{
626626
$Job = [System.Management.Automation.PowerShell]::Create().AddScript($ScriptBlock).AddParameters($ScriptParams)
627627
$Job.RunspacePool = $RunspacePool
628628

629-
$JobObj = New-Object PSObject -Property @{
629+
$JobObj = [pscustomobject] @{
630630
RunNum = $i - $StartIPv4Address_Int64
631631
Pipe = $Job
632632
Result = $Job.BeginInvoke()
633633
}
634634

635635
# Add Job to collection
636-
$Jobs.Add($JobObj) | Out-Null
636+
[void]$Jobs.Add($JobObj)
637637
}
638638

639639
Write-Verbose "Waiting for jobs to complete & starting to process results..."
640-
640+
641+
$Jobs_Total = $Jobs.Count
642+
641643
# Process results (that are finished), while waiting for other jobs
642644
Do {
643-
Write-Progress -Activity "Waiting for jobs to complete... ($($Threads - $($RunspacePool.GetAvailableRunspaces())) of $Threads threads running)" -Id 1 -PercentComplete (($Jobs.count - $($($Jobs | Where-Object {$_.Result.IsCompleted -eq $false}).Count)) / $Jobs.Count * 100) -Status "$(@($($Jobs | Where-Object {$_.Result.IsCompleted -eq $false})).Count) remaining..."
644-
645645
# Get all complete jobs
646-
$Jobs_ToProcess = $Jobs | Where {$_.Result.IsCompleted -eq $true}
647-
646+
$Jobs_ToProcess = $Jobs | Where-Object {$_.Result.IsCompleted}
647+
648648
# If no jobs finished yet, wait 500 ms and try again
649649
if($Jobs_ToProcess -eq $null)
650650
{
@@ -654,6 +654,16 @@ Process{
654654
continue
655655
}
656656

657+
# Catch when trying to divide through zero
658+
try {
659+
$Progress_Percent = 100 - ((($Jobs | Where-Object {$_.Result.IsCompleted -eq $false}).Count / $Jobs_Total) * 100)
660+
}
661+
catch {
662+
$Progress_Percent = 100
663+
}
664+
665+
Write-Progress -Activity "Waiting for jobs to complete... ($($Threads - $($RunspacePool.GetAvailableRunspaces())) of $Threads threads running)" -Id 1 -PercentComplete $Progress_Percent -Status "$(@($($Jobs | Where-Object {$_.Result.IsCompleted -eq $false})).Count) remaining..."
666+
657667
Write-Verbose "Processing $($Jobs_ToProcess.Count + 1) job(s)..."
658668

659669
# Processing completed jobs

0 commit comments

Comments
 (0)