Functions/Network/Convert-SubnetMaskToBits.ps1

Function Convert-SubnetMaskToBits
{
    [Cmdletbinding()]
    Param()
    DynamicParam
    {
        # Get list of Subnet Masks
        $Masks = Get-SubnetMask

        # Instantiate Runtime Parameter Dictionary, Attach Runtime Parameters, and return
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $RuntimeParameterDictionary.Add('SubnetMask', (New-DynamicParameter -ParamName 'SubnetMask' -ValueType string[] -Dataset $Masks -Mandatory $False -ValueFromPipeLine:$True))
        $RuntimeParameterDictionary.Add('Spaced', (New-DynamicParameter -ParamName 'Spaced' -ValueType bool -Dataset @($True,$False) -DefaultValue $False -Mandatory $False))
        return $RuntimeParameterDictionary
    }

    Process
        {
        # Convert Runtime Parameter Dictionary into Available Constants
        foreach ($key in $RuntimeParameterDictionary.keys){New-Variable -Name $key -Value $RuntimeParameterDictionary.$key.value}
        
        foreach ($SNM in $SubnetMask)
        {
            $TrueIP = try{[Net.IPAddress]::Parse($SNM)}catch{$null}
            if($TrueIP)
            {
                $INT64 = Convert-IPtoInt64 -IPAddress $TrueIP.IPAddressToString
                $BinaryMask = [convert]::ToString($INT64,2)
                switch ($Spaced)
                {
                    $TRUE {$Sects = $BinaryMask -split '(\w{8})' | where {$_}; "$($Sects[0]) $($Sects[1]) $($Sects[2]) $($Sects[3])"}
                    $FALSE {$BinaryMask}
                    DEFAULT {$BinaryMask}
                }
                
            }
            else {Write-Host "Could not convert SubnetMask ($SNM) to BITS" -ForegroundColor Red}
        }       
    }
}