Skip to content

Commit e1cdba4

Browse files
committed
Code improved + syntax fix
1 parent 1a38a35 commit e1cdba4

File tree

5 files changed

+30
-31
lines changed

5 files changed

+30
-31
lines changed
33.3 KB
Loading
-35.7 KB
Binary file not shown.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ This powerful asynchronus IPv4 Network Scanner for PowerShell allows you to scan
88

99
The default result will contain the the IPv4-Address, Status (Up or Down) and the Hostname. Other values can be displayed via parameter (Try Get-Help for more details).
1010

11-
![Screenshot](Documentation/Images/New-IPv4NetworkScan.png?raw=true "New-IPv4NetworkScan")
11+
![Screenshot](Documentation/Images/IPv4NetworkScan.png?raw=true "New-IPv4NetworkScan")
1212

1313
To reach the best possible performance, this script uses a [RunspacePool](https://msdn.microsoft.com/en-US/library/system.management.automation.runspaces.runspacepool(v=vs.85).aspx). As you can see in the following screenshot, the individual tasks are distributed across all cpu cores:
1414

15-
![Screenshot](Documentation/Images/New-IPv4NetworkScan_CPUusage.png?raw=true "CPU usage")
15+
![Screenshot](Documentation/Images/IPv4NetworkScan_CPUusage.png?raw=true "CPU usage")
1616

1717
If you are looking for a module containing this script... you can find it [here](https://github.com/BornToBeRoot/PowerShell)!
1818

@@ -21,25 +21,25 @@ Maybe you're also interested in my asynchronus [IPv4 Port Scanner](https://githu
2121
## Syntax
2222

2323
```powershell
24-
.\New-IPv4NetworkScan.ps1 [-StartIPv4Address] <IPAddress> [-EndIPv4Address] <IPAddress> [[-Tries] <Int32>] [[-Threads] <Int32>] [[-DisableDNSResolving]] [[-EnableMACResolving]] [[-ExtendedInformations]] [[-IncludeInactive]] [[-UpdateList]] [<CommonParameters>]
24+
.\IPv4NetworkScan.ps1 [-StartIPv4Address] <IPAddress> [-EndIPv4Address] <IPAddress> [[-Tries] <Int32>] [[-Threads] <Int32>] [[-DisableDNSResolving]] [[-EnableMACResolving]] [[-ExtendedInformations]] [[-IncludeInactive]] [[-UpdateList]] [<CommonParameters>]
2525
26-
.\New-IPv4NetworkScan.ps1 [-IPv4Address] <IPAddress> [-Mask] <String> [[-Tries] <Int32>] [[-Threads] <Int32>] [[-DisableDNSResolving]] [[-EnableMACResolving]] [[-ExtendedInformations]] [[-IncludeInactive]] [[-UpdateList]] [<CommonParameters>]
26+
.\IPv4NetworkScan.ps1 [-IPv4Address] <IPAddress> [-Mask] <String> [[-Tries] <Int32>] [[-Threads] <Int32>] [[-DisableDNSResolving]] [[-EnableMACResolving]] [[-ExtendedInformations]] [[-IncludeInactive]] [[-UpdateList]] [<CommonParameters>]
2727
28-
.\New-IPv4NetworkScan.ps1 [-IPv4Address] <IPAddress> [-CIDR] <Int32> [[-Tries] <Int32>] [[-Threads] <Int32>] [[-DisableDNSResolving]] [[-EnableMACResolving]] [[-ExtendedInformations]] [[-IncludeInactive]] [[-UpdateList]] [<CommonParameters>]
28+
.\IPv4NetworkScan.ps1 [-IPv4Address] <IPAddress> [-CIDR] <Int32> [[-Tries] <Int32>] [[-Threads] <Int32>] [[-DisableDNSResolving]] [[-EnableMACResolving]] [[-ExtendedInformations]] [[-IncludeInactive]] [[-UpdateList]] [<CommonParameters>]
2929
```
3030

3131
## Example
3232

3333
```powershell
34-
PS> .\New-IPv4NetworkScan.ps1 -StartIPv4Address 192.168.178.0 -EndIPv4Address 192.168.178.20
34+
PS> .\IPv4NetworkScan.ps1 -StartIPv4Address 192.168.178.0 -EndIPv4Address 192.168.178.20
3535
3636
IPv4Address Status Hostname
3737
----------- ------ --------
3838
192.168.178.1 Up fritz.box
3939
```
4040

4141
```powershell
42-
PS> .\New-IPv4NetworkScan.ps1 -IPv4Address 192.168.178.0 -Mask 255.255.255.0 -DisableDNSResolving
42+
PS> .\IPv4NetworkScan.ps1 -IPv4Address 192.168.178.0 -Mask 255.255.255.0 -DisableDNSResolving
4343
4444
IPv4Address Status
4545
----------- ------
@@ -48,7 +48,7 @@ IPv4Address Status
4848
```
4949

5050
```powershell
51-
PS> .\New-IPv4NetworkScan.ps1 -IPv4Address 192.168.178.0 -CIDR 25 -EnableMACResolving
51+
PS> .\IPv4NetworkScan.ps1 -IPv4Address 192.168.178.0 -CIDR 25 -EnableMACResolving
5252
5353
IPv4Address Status Hostname MAC Vendor
5454
----------- ------ -------- --- ------

Scripts/New-IPv4NetworkScan.ps1 renamed to Scripts/IPv4NetworkScan.ps1

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
###############################################################################################################
22
# Language : PowerShell 4.0
3-
# Filename : New-IPv4NetworkScan.ps1
3+
# Filename : IPv4NetworkScan.ps1
44
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
55
# Description : Powerful asynchronus IPv4 Network Scanner
66
# Repository : https://github.com/BornToBeRoot/PowerShell_IPv4NetworkScanner
@@ -211,12 +211,12 @@ Begin{
211211

212212
# Split into groups of 8 bits, convert to Ints, join up into a string
213213
$Octets = $CIDR_Bits -split '(.{8})' -ne ''
214-
$Mask = ($Octets | foreach { [Convert]::ToInt32($_, 2) }) -join '.'
214+
$Mask = ($Octets | ForEach-Object { [Convert]::ToInt32($_, 2) }) -join '.'
215215
}
216216

217217
"Mask" {
218218
# Convert the numbers into 8 bit blocks, join them all together, count the 1
219-
$Octets = $Mask.ToString().Split(".") | foreach {[Convert]::ToString($_, 2)}
219+
$Octets = $Mask.ToString().Split(".") | ForEach-Object {[Convert]::ToString($_, 2)}
220220
$CIDR_Bits = ($Octets -join "").TrimEnd("0")
221221

222222
# Count the "1" (111111111111111111111111 --> /24)
@@ -287,7 +287,7 @@ Begin{
287287
}
288288

289289
# Helper function to create a new Subnet
290-
function New-IPv4Subnet
290+
function Get-IPv4Subnet
291291
{
292292
[CmdletBinding(DefaultParameterSetName='CIDR')]
293293
param(
@@ -440,7 +440,7 @@ Process{
440440
}
441441

442442
# Create new subnet
443-
$Subnet = New-IPv4Subnet -IPv4Address $IPv4Address -CIDR $CIDR
443+
$Subnet = Get-IPv4Subnet -IPv4Address $IPv4Address -CIDR $CIDR
444444

445445
# Assign Start and End IPv4-Address
446446
$StartIPv4Address = $Subnet.NetworkID
@@ -552,7 +552,7 @@ Process{
552552
# +++ Get MAC-Address +++
553553
$MAC = [String]::Empty
554554

555-
if(($EnableMACResolving) -and ($Status -eq "Up"))
555+
if(($EnableMACResolving) -and (($Status -eq "Up") -or ($IncludeInactive)))
556556
{
557557
$Arp_Result = (arp -a ).ToUpper()
558558

@@ -588,27 +588,26 @@ Process{
588588
$ResponseTime = $PingResult.RoundtripTime
589589
$TTL = $PingResult.Options.Ttl
590590
}
591-
catch{} # Failed to get extended informations
591+
catch{ } # Failed to get extended informations
592592
}
593593

594594
# +++ Result +++
595-
if($Status -eq "Up" -or $IncludeInactive)
595+
596+
if(($Status -eq "Up") -or ($IncludeInactive))
596597
{
597-
$Result = [pscustomobject] @{
598+
[pscustomobject] @{
598599
IPv4Address = $IPv4Address
599600
Status = $Status
600601
Hostname = $Hostname
601602
MAC = $MAC
602-
BufferSize = $BufferSize
603-
ResponseTime = $ResponseTime
604-
TTL = $TTL
603+
BufferSize = $BufferSize
604+
ResponseTime = $ResponseTime
605+
TTL = $TTL
605606
}
606-
607-
return $Result
608-
}
609-
else
607+
}
608+
else
610609
{
611-
return $null
610+
$null
612611
}
613612
}
614613

@@ -661,7 +660,7 @@ Process{
661660
[void]$Jobs.Add($JobObj)
662661
}
663662

664-
Write-Verbose "Waiting for jobs to complete & starting to process results..."
663+
Write-Verbose -Message "Waiting for jobs to complete & starting to process results..."
665664

666665
# Total jobs to calculate percent complete, because jobs are removed after they are processed
667666
$Jobs_Total = $Jobs.Count
@@ -672,9 +671,9 @@ Process{
672671
$Jobs_ToProcess = $Jobs | Where-Object {$_.Result.IsCompleted}
673672

674673
# If no jobs finished yet, wait 500 ms and try again
675-
if($Jobs_ToProcess -eq $null)
674+
if($null -eq $Jobs_ToProcess)
676675
{
677-
Write-Verbose "No jobs completed, wait 500ms..."
676+
Write-Verbose -Message "No jobs completed, wait 500ms..."
678677

679678
Start-Sleep -Milliseconds 500
680679
continue
@@ -693,7 +692,7 @@ Process{
693692

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

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

698697
# Processing completed jobs
699698
foreach($Job in $Jobs_ToProcess)
@@ -705,8 +704,8 @@ Process{
705704
# Remove job from collection
706705
$Jobs.Remove($Job)
707706

708-
# Check if result is null --> if not, return it
709-
if($Job_Result -ne $null)
707+
# Check if result contains status
708+
if($Job_Result.Status)
710709
{
711710
if($AssignVendorToMAC)
712711
{

0 commit comments

Comments
 (0)