This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Split-IPv4Subnet.ps1
136 lines (113 loc) · 4.51 KB
/
Split-IPv4Subnet.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Split-IPv4Subnet.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Split a subnet in multiple subnets with given subnetmasks
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Split a subnet in multiple subnets with given subnetmasks
.DESCRIPTION
Split a subnet in multiple subnets with given subnetmasks. Each new subnet contains NetworkID, Broadcast, total available IPs and usable IPs for hosts.
.EXAMPLE
Split-IPv4Subnet -IPv4Address 192.168.0.0 -CIDR 22 -NewCIDR 24
NetworkID Broadcast IPs Hosts
--------- --------- --- -----
192.168.0.0 192.168.0.255 256 254
192.168.1.0 192.168.1.255 256 254
192.168.2.0 192.168.2.255 256 254
192.168.3.0 192.168.3.255 256 254
.EXAMPLE
Split-IPv4Subnet -IPv4Address 192.168.0.0 -Mask 255.255.255.0 -NewMask 255.255.255.128
NetworkID Broadcast IPs Hosts
--------- --------- --- -----
192.168.0.0 192.168.0.127 128 126
192.168.0.128 192.168.0.255 128 126
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/Split-IPv4Subnet.README.md
#>
function Split-IPv4Subnet
{
[CmdletBinding(DefaultParameterSetName='CIDR')]
param(
[Parameter(
Position=0,
Mandatory=$true,
HelpMessage='IPv4-Address which is in the subnet')]
[IPAddress]$IPv4Address,
[Parameter(
ParameterSetName='CIDR',
Position=1,
Mandatory=$true,
HelpMessage='CIDR like /24 without "/"')]
[ValidateRange(0,31)]
[Int32]$CIDR,
[Parameter(
ParameterSetName='CIDR',
Position=2,
Mandatory=$true,
HelpMessage='New CIDR like /28 without "/"')]
[ValidateRange(0,31)]
[Int32]$NewCIDR,
[Parameter(
ParameterSetName='Mask',
Position=1,
Mandatory=$true,
Helpmessage='Subnetmask like 255.255.255.0')]
[ValidateScript({
if($_ -match "^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(254|252|248|240|224|192|128|0)$")
{
return $true
}
else {
throw "Enter a valid subnetmask (like 255.255.255.0)!"
}
})]
[String]$Mask,
[Parameter(
ParameterSetName='Mask',
Position=2,
Mandatory=$true,
HelpMessage='Subnetmask like 255.255.255.128')]
[ValidateScript({
if($_ -match "^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(254|252|248|240|224|192|128|0)$")
{
return $true
}
else
{
throw "Enter a valid subnetmask (like 255.255.255.0)!"
}
})]
[String]$NewMask
)
Begin{
}
Process{
if($PSCmdlet.ParameterSetName -eq 'Mask')
{
$CIDR = (Convert-Subnetmask -Mask $Mask).CIDR
$NewCIDR = (Convert-Subnetmask -Mask $NewMask).CIDR
}
if($CIDR -ge $NewCIDR)
{
throw "Subnet (/$CIDR) can't be greater or equal than new subnet (/$NewCIDR)"
}
# Calculate the current Subnet
$Subnet = Get-IPv4Subnet -IPv4Address $IPv4Address -CIDR $CIDR
# Get new HostBits based on SubnetBits (CIDR) // Hostbits (32 - /24 = 8 -> 00000000000000000000000011111111)
$NewHostBits = ('1' * (32 - $NewCIDR)).PadLeft(32, "0")
# Convert Bits to Int64, add +1 to get all available IPs
$NewAvailableIPs = ([Convert]::ToInt64($NewHostBits,2) + 1)
# Convert the NetworkID to Int64
$NetworkID_Int64 = (Convert-IPv4Address -IPv4Address $Subnet.NetworkID).Int64
# Build new subnets, and return them
for($i = 0; $i -lt $Subnet.IPs;$i += $NewAvailableIPs)
{
Get-IPv4Subnet -IPv4Address (Convert-IPv4Address -Int64 ($NetworkID_Int64 + $i)).IPv4Address -CIDR $NewCIDR
}
}
End{
}
}