Private/ConvertTo-CIDR.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
function ConvertTo-CIDR {
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param (
        [Parameter(Mandatory)]
        [string] $IPAddress,
        [Parameter(Mandatory)]
        [string] $SubnetMask
    )

    $ip = [IPAddress] $IPAddress
    $mask = [IPAddress] $SubnetMask
    [void] ($mask.IPAddressToString -match '(.*)\.(.*)\.(.*)\.(.*)')
    $suffix = ""
    $Matches[1..4] | ForEach-Object {
        $suffix += [Convert]::ToString([int] $_, 2) + ("0" * (8 - [Convert]::ToString([int] $_, 2).Length))
    }
    $suffix = ($suffix -split "[^1]")[0].Length

    @{
        IPAddres   = $IPAddress
        CIDR       = "$IPAddress/$suffix"
        CIDRSuffix = $suffix
        NetworkID  = ([IPAddress] ($ip.Address -band $mask.Address)).IPAddressToString
        SubnetMask = $mask.IPAddressToString
    }
}