-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network-Class.ps1
84 lines (72 loc) · 2.2 KB
/
Network-Class.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Class Network{
#Beginning and end of IP range in a given Subnet, as well as the subnet and IPRange as an array of strings.
[String] $StartIP
[String] $EndIP
[String] $Subnet
[String] $FirstSubnet
[String] $LastSubnet
[String[]] $IPrange
#Constructor to be used when working with only one subnet.
Network($StartIP, $EndIP){
$This.StartIP = $StartIP
$This.EndIP = $EndIP
}
#Constructor to be used when working with multiple subnets.
Network($StartIP, $EndIP, $FirstSubnet, $LastSubnet){
$This.StartIP = $StartIP
$This.EndIP = $EndIP
$This.FirstSubnet = $FirstSubnet
$This.LastSubnet = $LastSubnet
}
function Get-IPs {
[cmdletbinding()]
Param(
[Parameter(Mandatory = $True,
Position =0,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True,
HelpMessage ="Specify the first IP in the range(s).")]
[Alias('Start')]
[String] $StartIP,
[Parameter(Mandatory = $True,
Position =1,
HelpMessage ="Enter the last IP in the range(s).")]
[Alias('End')]
[String] $EndIP
)
[Int]$BitCount = 0
[String]$Subnet
for([Int] $i = 0, $i -LT $StartIP.Length; $i++){
if($StartIP[$i] -EQ "."){
$BitCount++
}
if ($BitCount -EQ 3) {
$Subnet = $StartIP
[Int] $StartLastBit = $StartIP.Substring($i)
break
}
}
for([Int] $j = 0, $j -LT $EndIP.Length; $j++){
if($EndIP[$j] -EQ "."){
$BitCount++
}
if ($BitCount -EQ 3) {
[Int] $EndLastBit = $EndIP.Substring($j)
$BitCount = 0
break
}
}
for([Int] $k = $Subnet.Length; $k -GE 0; $k--){
$Subnet = $Subnet -Replace ".$"
if($Subnet[$k] -EQ "."){
break
}
}
$IPrange = "$($Subnet) $($StartLastBit..$EndLastBit)"
foreach($IP in $IPrange) {
Test-Connection $IP -Count 1 -Quiet | Where-Object {$_ -EQ "True"}
$Properties = @{IPAddress = $IP
Status = 'Connected'
}
}
}