1
# (c) 2008-2014 Chris Dent.
3
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without
4
# fee is hereby granted, provided that the above copyright notice and this permission notice appear
7
# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
8
# SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
9
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
11
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
15
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
16
function ConvertTo-DecimalIP {
19
Converts a Decimal IP address into a 32-bit unsigned integer.
21
ConvertTo-DecimalIP takes a decimal IP, uses a shift-like operation on each octet and returns a single UInt32 value.
23
An IP Address to convert.
28
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
29
[Net.IPAddress]$IPAddress
33
$i = 3; $DecimalIP = 0;
34
$IPAddress.GetAddressBytes() | ForEach-Object { $DecimalIP += $_ * [Math]::Pow(256, $i); $i-- }
36
return [UInt32]$DecimalIP
40
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
41
function ConvertTo-MaskLength {
44
Returns the length of a subnet mask.
46
ConvertTo-MaskLength accepts any IPv4 address as input, however the output value
47
only makes sense when using a subnet mask.
49
A subnet mask to convert into length
54
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
56
[Net.IPAddress]$SubnetMask
60
$Bits = "$( $SubnetMask.GetAddressBytes() | ForEach-Object { [Convert]::ToString($_, 2) } )" -replace '[\s0]'
66
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
67
function ConvertTo-DottedDecimalIP {
70
Returns a dotted decimal IP address from either an unsigned 32-bit integer or a dotted binary string.
72
ConvertTo-DottedDecimalIP uses a regular expression match on the input string to convert to an IP address.
74
A string representation of an IP address from either UInt32 or dotted binary.
79
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
84
Switch -RegEx ($IPAddress) {
85
"([01]{8}.){3}[01]{8}" {
86
return [String]::Join('.', $( $IPAddress.Split('.') | ForEach-Object { [Convert]::ToUInt32($_, 2) } ))
89
$IPAddress = [UInt32]$IPAddress
90
$DottedIP = $( For ($i = 3; $i -gt -1; $i--) {
91
$Remainder = $IPAddress % [Math]::Pow(256, $i)
92
($IPAddress - $Remainder) / [Math]::Pow(256, $i)
93
$IPAddress = $Remainder
96
return [String]::Join('.', $DottedIP)
99
Write-Error "Cannot convert this format"
105
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
106
function ConvertTo-Mask {
109
Returns a dotted decimal subnet mask from a mask length.
111
ConvertTo-Mask returns a subnet mask in dotted decimal format from an integer value ranging
112
between 0 and 32. ConvertTo-Mask first creates a binary string from the length, converts
113
that to an unsigned 32-bit integer then calls ConvertTo-DottedDecimalIP to complete the operation.
114
.Parameter MaskLength
115
The number of bits which must be masked.
120
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
122
[ValidateRange(0, 32)]
127
return ConvertTo-DottedDecimalIP ([Convert]::ToUInt32($(("1" * $MaskLength).PadRight(32, "0")), 2))
131
# Original work found at: http://www.indented.co.uk/2010/01/23/powershell-subnet-math/
132
function Get-NetworkAddress {
135
Takes an IP address and subnet mask then calculates the network address for the range.
137
Get-NetworkAddress returns the network address for a subnet by performing a bitwise AND
138
operation against the decimal forms of the IP address and subnet mask. Get-NetworkAddress
139
expects both the IP address and subnet mask in dotted decimal format.
141
Any IP address within the network range.
142
.Parameter SubnetMask
143
The subnet mask for the network.
148
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
149
[Net.IPAddress]$IPAddress,
151
[Parameter(Mandatory = $true, Position = 1)]
153
[Net.IPAddress]$SubnetMask
157
return ConvertTo-DottedDecimalIP ((ConvertTo-DecimalIP $IPAddress) -band (ConvertTo-DecimalIP $SubnetMask))
161
Export-ModuleMember -Function * -Alias *
b'\\ No newline at end of file'