Skip to content

Commit b3a3a3c

Browse files
committed
New-Object replaced with [pscustomobject]
1 parent edc8e78 commit b3a3a3c

File tree

1 file changed

+78
-44
lines changed

1 file changed

+78
-44
lines changed

Scripts/New-IPv4NetworkScan.ps1

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,10 @@ Begin{
234234
}
235235
}
236236

237-
$Result = New-Object -TypeName PSObject
238-
Add-Member -InputObject $Result -MemberType NoteProperty -Name Mask -Value $Mask
239-
Add-Member -InputObject $Result -MemberType NoteProperty -Name CIDR -Value $CIDR
237+
$Result = [pscustomobject] @{
238+
Mask = $Mask
239+
CIDR = $CIDR
240+
}
240241

241242
return $Result
242243
}
@@ -286,9 +287,10 @@ Begin{
286287
}
287288
}
288289

289-
$Result = New-Object -TypeName PSObject
290-
Add-Member -InputObject $Result -MemberType NoteProperty -Name IPv4Address -Value $IPv4Address
291-
Add-Member -InputObject $Result -MemberType NoteProperty -Name Int64 -Value $Int64
290+
$Result = [pscustomobject] @{
291+
IPv4Address = $IPv4Address
292+
Int64 = $Int64
293+
}
292294

293295
return $Result
294296
}
@@ -370,11 +372,12 @@ Begin{
370372
$Hosts = ($AvailableIPs - 2)
371373

372374
# Build custom PSObject
373-
$Result = New-Object -TypeName PSObject
374-
Add-Member -InputObject $Result -MemberType NoteProperty -Name NetworkID -Value $NetworkID
375-
Add-Member -InputObject $Result -MemberType NoteProperty -Name Broadcast -Value $Broadcast
376-
Add-Member -InPutObject $Result -MemberType NoteProperty -Name IPs -Value $AvailableIPs
377-
Add-Member -InPutObject $Result -MemberType NoteProperty -Name Hosts -Value $Hosts
375+
$Result = [pscustomobject] @{
376+
NetworkID = $NetworkID
377+
Broadcast = $Broadcast
378+
IPs = $AvailableIPs
379+
Hosts = $Hosts
380+
}
378381

379382
return $Result
380383
}
@@ -388,7 +391,7 @@ Begin{
388391
function AssignVendorToMAC
389392
{
390393
param(
391-
[PSObject]$Result
394+
$Result
392395
)
393396

394397
Begin{
@@ -410,10 +413,18 @@ Begin{
410413
catch {}
411414
}
412415

413-
$NewResult = New-Object -TypeName PSObject -ArgumentList $Result
414-
Add-Member -InputObject $NewResult -MemberType NoteProperty -Name Vendor -Value $Vendor
415-
416-
return $NewResult
416+
$NewResult = [pscustomobject] @{
417+
IPv4Address = $Result.IPv4Address
418+
Status = $Result.Status
419+
Hostname = $Result.Hostname
420+
MAC = $Result.MAC
421+
Vendor = $Vendor
422+
BufferSize = $Result.BufferSize
423+
ResponseTime = $Result.ResponseTime
424+
TTL = $Result.TTL
425+
}
426+
427+
return $NewResult
417428
}
418429

419430
End {
@@ -458,19 +469,39 @@ Process{
458469
Write-Verbose "Running with max $Threads threads"
459470
Write-Verbose "ICMP checks per IP is set to $Tries"
460471

461-
# Check if it is possible to assign vendor to MAC and import CSV-File
472+
# Properties which are displayed in the output
473+
$PropertiesToDisplay = @()
474+
$PropertiesToDisplay += "IPv4Address", "Status"
475+
476+
if($DisableDNSResolving.IsPresent -eq $false)
477+
{
478+
$PropertiesToDisplay += "Hostname"
479+
}
480+
481+
if($EnableMACResolving.IsPresent)
482+
{
483+
$PropertiesToDisplay += "MAC"
484+
}
485+
486+
# Check if it is possible to assign vendor to MAC --> import CSV-File
462487
if(($EnableMACResolving.IsPresent) -and ([System.IO.File]::Exists($CSV_MACVendorList_Path)))
463488
{
464489
$AssignVendorToMAC = $true
465490

466-
# Import the CSV-File
491+
$PropertiesToDisplay += "Vendor"
492+
467493
$MAC_VendorList = Import-Csv -Path $CSV_MACVendorList_Path | Select-Object "Assignment", "Organization Name"
468494
}
469495
else
470496
{
471497
$AssignVendorToMAC = $false
472498
}
473499

500+
if($ExtendedInformations.IsPresent)
501+
{
502+
$PropertiesToDisplay += "BufferSize", "ResponseTime", "TTL"
503+
}
504+
474505
# Scriptblock --> will run in runspaces (threads)...
475506
[System.Management.Automation.ScriptBlock]$ScriptBlock = {
476507
Param(
@@ -481,11 +512,7 @@ Process{
481512
$ExtendedInformations,
482513
$IncludeInactive
483514
)
484-
485-
# Built custom PSObject
486-
$Result = New-Object -TypeName PSObject
487-
Add-Member -InputObject $Result -MemberType NoteProperty -Name IPv4Address -Value $IPv4Address
488-
515+
489516
# +++ Send ICMP requests +++
490517
$Status = [String]::Empty
491518

@@ -515,9 +542,7 @@ Process{
515542
break # Exit loop, if there is an error
516543
}
517544
}
518-
519-
Add-Member -InputObject $Result -MemberType NoteProperty -Name Status -Value $Status
520-
545+
521546
# +++ Resolve DNS +++
522547
$Hostname = [String]::Empty
523548

@@ -526,9 +551,7 @@ Process{
526551
try{
527552
$Hostname = ([System.Net.Dns]::GetHostEntry($IPv4Address).HostName)
528553
}
529-
catch { } # No DNS
530-
531-
Add-Member -InputObject $Result -MemberType NoteProperty -Name Hostname -Value $Hostname
554+
catch { } # No DNS
532555
}
533556

534557
# +++ Get MAC-Address +++
@@ -556,29 +579,36 @@ Process{
556579
catch{ } # No MAC
557580
}
558581

559-
Add-Member -InputObject $Result -MemberType NoteProperty -Name MAC -Value $MAC
560582
}
561583

562584
# +++ Get extended informations +++
563585
$BufferSize = [String]::Empty
564586
$ResponseTime = [String]::Empty
565-
587+
$TTL = $null
588+
566589
if($ExtendedInformations.IsPresent -and ($Status -eq "Up"))
567590
{
568591
try{
569592
$BufferSize = $PingResult.Buffer.Length
570593
$ResponseTime = $PingResult.RoundtripTime
571594
$TTL = $PingResult.Options.Ttl
572595
}
573-
catch{} # Failed to get extended informations
574-
575-
Add-Member -InputObject $Result -MemberType NoteProperty -Name BufferSize -Value $BufferSize
576-
Add-Member -InputObject $Result -MemberType NoteProperty -Name ResponseTime -Value $ResponseTime
577-
Add-Member -InputObject $Result -MemberType NoteProperty -Name TTL -Value $TTL
596+
catch{} # Failed to get extended informations
578597
}
579598

599+
# +++ Result +++
580600
if($Status -eq "Up" -or $IncludeInactive.IsPresent)
581601
{
602+
$Result = [pscustomobject] @{
603+
IPv4Address = $IPv4Address
604+
Status = $Status
605+
Hostname = $Hostname
606+
MAC = $MAC
607+
BufferSize = $BufferSize
608+
ResponseTime = $ResponseTime
609+
TTL = $TTL
610+
}
611+
582612
return $Result
583613
}
584614
else
@@ -596,7 +626,7 @@ Process{
596626

597627
Write-Verbose "Setting up Jobs..."
598628

599-
# Set up Jobs for each IP
629+
# Set up Jobs for each IP...
600630
for ($i = $StartIPv4Address_Int64; $i -le $EndIPv4Address_Int64; $i++)
601631
{
602632
# Convert IP back from Int64
@@ -638,11 +668,12 @@ Process{
638668

639669
Write-Verbose "Waiting for jobs to complete & starting to process results..."
640670

671+
# Total jobs to calculate percent complete, because jobs are removed after they are processed
641672
$Jobs_Total = $Jobs.Count
642673

643-
# Process results (that are finished), while waiting for other jobs
674+
# Process results, while waiting for other jobs
644675
Do {
645-
# Get all complete jobs
676+
# Get all jobs, which are completed
646677
$Jobs_ToProcess = $Jobs | Where-Object {$_.Result.IsCompleted}
647678

648679
# If no jobs finished yet, wait 500 ms and try again
@@ -653,16 +684,19 @@ Process{
653684
Start-Sleep -Milliseconds 500
654685
continue
655686
}
687+
688+
# Get jobs, which are not complete yet
689+
$Jobs_Remaining = ($Jobs | Where-Object {$_.Result.IsCompleted -eq $false}).Count
656690

657691
# Catch when trying to divide through zero
658-
try {
659-
$Progress_Percent = 100 - ((($Jobs | Where-Object {$_.Result.IsCompleted -eq $false}).Count / $Jobs_Total) * 100)
692+
try {
693+
$Progress_Percent = 100 - (($Jobs_Remaining / $Jobs_Total) * 100)
660694
}
661695
catch {
662696
$Progress_Percent = 100
663697
}
664698

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..."
699+
Write-Progress -Activity "Waiting for jobs to complete... ($($Threads - $($RunspacePool.GetAvailableRunspaces())) of $Threads threads running)" -Id 1 -PercentComplete $Progress_Percent -Status "$Jobs_Remaining remaining..."
666700

667701
Write-Verbose "Processing $($Jobs_ToProcess.Count + 1) job(s)..."
668702

@@ -681,11 +715,11 @@ Process{
681715
{
682716
if($AssignVendorToMAC)
683717
{
684-
AssignVendorToMAC($Job_Result)
718+
AssignVendorToMAC -Result $Job_Result | Select-Object -Property $PropertiesToDisplay
685719
}
686720
else
687721
{
688-
$Job_Result
722+
$Job_Result | Select-Object -Property $PropertiesToDisplay
689723
}
690724
}
691725
}

0 commit comments

Comments
 (0)